How does a pointer to a struct or array value in G

2019-06-14 16:10发布

问题:

Considering the following Go struct:

type Person struct {
    Name    string
    Age     int
    Country string
}

I have encountered numerious times the following use:

p := &Person{"Adam", 33, "Argentina"}

Yet I can not see the point in pointing to a struct value, and I wonder, how does it differ from:

n := &999 // Error

My questions are:

  1. How is it even possible to point to a value, even if it is a struct or array and not a primitive like a string or int? Strange enough, the following doesn't contribute to my understanding:

    fmt.Println(p, &p) // outputs: &{Adam 33 Argentina} 0xc042084018
    
  2. Why would a programmer want to declare a struct instance by a pointer? What could you achieve doing so?

回答1:

&Person{} is a language "construct", it's part of the spec: it allocates a new variable of Person type, and provides you the address of that anonymous variable.

Spec: Composite literals:

Taking the address of a composite literal generates a pointer to a unique variable initialized with the literal's value.

Also: Spec: Variables:

Calling the built-in function new or taking the address of a composite literal allocates storage for a variable at run time.

&999 is not allowed by the language spec. The possible operands of the address operators are listed in the Spec: Address operators:

The operand must be addressable, that is, either a variable, pointer indirection, or slice indexing operation; or a field selector of an addressable struct operand; or an array indexing operation of an addressable array. As an exception to the addressability requirement, x may also be a (possibly parenthesized) composite literal.

p := Person{} creates a new variable p whose type will be Person. p := &Person{} creates a new variable whose type will be *Person.

See possible duplicate: How do I do a literal *int64 in Go?

When you print the values with the fmt package, it has certain rules how to print values of different types:

For compound objects, the elements are printed using these rules, recursively, laid out like this:

struct:             {field0 field1 ...}
array, slice:       [elem0 elem1 ...]
maps:               map[key1:value1 key2:value2]
pointer to above:   &{}, &[], &map[]

When you use fmt.Println(), the default formatting rules will be applied, which for a value of type *int is the %p verb, which will print the memory address in hexadecimal format, but for a pointer to struct it prints the struct value prepended with an & sign (&{}). You can read more about it in related question: Difference between golang pointers

If you want to print the pointed value, dereference the pointer and pass the pointed value, e.g.:

var p = new(int)
*p = 12
fmt.Println(*p) // Prints 12

As to why to create a pointer to a value (and not a value), see these related questions:

Pointers vs. values in parameters and return values

Why should constructor of Go return address?

Go, X does not implement Y (... method has a pointer receiver)