Go lang Адреса, указатели и типы:
s := "hello" // type string
t := "bye" // type string
u := 44 // type int
v := [2]int{1, 2} // type array
Все эти переменные Go имеют адрес.Даже переменные типа «указатель» имеют адреса.Различие состоит в том, что строковые типы содержат строковые значения, типы int содержат целочисленные значения, а типы указателей содержат адреса.
&
== оцените адрес или подумайте: «вот мой адрес, чтобы вы знали, где меня найти»
// make p type pointer (to string only) and assign value to address of s
var p *string = &s // type *string
// or
q := &s // shorthand, same deal
*
== разыменовать указатель или подумать «передать действие по адресу, который является моим значением»
*p = "ciao" // change s, not p, the value of p remains the address of s
// j := *s // error, s is not a pointer type, no address to redirect action to
// p = "ciao" // error, can't change to type string
p = &t // change p, now points to address of t
//p = &u // error, can't change to type *int
// make r type pointer (to pointer [to string]) and assign value to address of p
var r **string = &p // shorthand: r := &p
w := ( r == &p) // ( r evaluates to address of p) w = true
w = ( *r == p ) // ( *r evaluates to value of p [address of t]) w = true
w = (**r == t ) // (**r evaluates to value of t) w = true
// make n type pointer (to string) and assign value to address of t (deref'd p)
n := &*p
o := *&t // meaningless flip-flop, same as: o := t
// point y to array v
y := &v
z := (*y)[0] // dereference y, get first value of element, assign to z (z == 1)
Перейти Играть здесь: http://play.golang.org/p/u3sPpYLfz7