4 ms·
The articles states: >"It's OK to call a pointer receiver method on a value as long as the value is addressable. In other words, you don't need to have a value
by bogomipz 8y ago
The articles states:
>"It's OK to call a pointer receiver method on a value as long as the value is addressable. In other words, you don't need to have a value receiver version of the method in some cases.
Not every variable is addressable though. Map elements are not addressable."
Since everything in memory has an address I am confused by these statements. Does the author mean that "not everything can be pointed to" with a pointer variable in Go?
- bumbledraven 8y agoGo itself says you "cannot take the address" of some values. For example: m := map[int]int{1: 2} p := &m[1] The second line causes the compilation error "cannot take the address of m[1]". https://play.golang.org/p/pIMgeQzOSZK https://play.golang.org/p/pIMgeQzOSZK
- bogomipz 8y agoThanks, I think it was the wording "addressable" that was the source of my confusion. "cannot take the address of" seems more intuitive to me. Cheers.
- lenkite 8y agohttps://stackoverflow.com/questions/32495402/why-does-go-forbid-taking-the-address-of-map-member-yet-allows-slice-el https://stackoverflow.com/questions/32495402/why-does-go-for...