Go dereferencing pointers

I am confused why line 15 is not valid. Why can't there be a pointer to a big one. Not dereferenced while a pointer to int can?

package main import ( "fmt" "big" ) func main() { var c *int = getPtr() fmt.Println(c) fmt.Println(*c) var d *big.Int = big.NewInt(int64(0)) fmt.Println(d) // does not compile - implicit assignment of big.Int // field 'neg' in function argument //fmt.Println(*d) } func getPtr() *int { var a int = 0 var b *int = &a return b } 
+4
source share
1 answer

This is because Int is a structure with unfulfilled fields. When you pass a structure by function value, you make a copy of it. Go spec states that you need legal

... either all fields of T must be exported, or the assignment must be in the same package in which T is declared. In other words, the structure value can be assigned to the structure variable only if each field structure can be legally assigned individually by the program.

+5
source

Source: https://habr.com/ru/post/1303281/


All Articles