What is the meaning of '*' and '&'?

This is possibly one of the most confusing things in Go. There are basically 3 cases you need to understand:

The & Operator

& goes in front of a variable when you want to get that variable's memory address.

The * Operator

* goes in front of a variable that holds a memory address and resolves it (it is therefore the counterpart to the & operator). It goes and gets the thing that the pointer was pointing at, e.g. *myString.

myString := "Hi"
fmt.Println(*&myString)  // prints "Hi"

or more usefully, something like

myStructPointer = &myStruct
// ...
(*myStructPointer).someAttribute = "New Value"

* in front of a Type

When * is put in front of a type, e.g. *string, it becomes part of the type declaration, so you can say "this variable holds a pointer to a string". For example:

var str_pointer *string

So the confusing thing is that the * really gets used for 2 separate (albeit related) things. The star can be an operator or part of a type.


Your question doesn't match very well the example given but I'll try to be straightforward.

Let's suppose we have a variable named a which holds the integer 5 and another variable named p which is going to be a pointer. This is where the * and & come into the game.

Printing variables with them can generate different output, so it all depends on the situation and how well you use. The use of * and & can save you lines of code (that doesn't really matter in small projects) and make your code more beautiful/readable.

& returns the memory address of the following variable.

* returns the value of the following variable (which should hold the memory address of a variable, unless you want to get weird output and possibly problems because you're accessing your computer's RAM)

var a = 5
var p = &a // p holds variable a's memory address
fmt.Printf("Address of var a: %p\n", p)
fmt.Printf("Value of var a: %v\n", *p)

// Let's change a value (using the initial variable or the pointer)
*p = 3 // using pointer
a = 3 // using initial var

fmt.Printf("Address of var a: %p\n", p)
fmt.Printf("Value of var a: %v\n", *p)

All in all, when using * and & in remember that * is for setting the value of the variable you're pointing to and & is the address of the variable you're pointing to/want to point to.

Hope this answer helps.


Those are pointers like we have in C++.

The differences are:

  • Instead of -> to call a method on a pointer, you always use ., i.e. pointer.method().

  • There are no dangling pointers. It is perfectly valid to return a pointer to a local variable. Golang will ensure the lifetime of the object and garbage-collect it when it's no longer needed.

  • Pointers can be created with new() or by creating a object object{} and taking the address of it with &.

  • Golang does not allow pointer-arithmetic (arrays do not decay to pointers) and insecure casting. All downcasts will be checked using the runtime-type of the variable and either panic or return false as second return-value when the instance is of the wrong type, depending on whether you actually take the second return type or not.

Tags:

Go