Pointers

Pointers

In Go programming language, a pointer is a variable that stores the memory address of another variable. Pointers allow you to indirectly read or modify the value of the referenced variable without necessarily knowing its name.

You can declare a pointer type using the * symbol along with the associated type. For example, a pointer to an integer value looks like this:

var p *int

You can create a pointer from an existing variable using the & operator:

i := 39
p = &i

Alternatively, you can use the new built-in function to create a pointer initialized with the default value for the pointer type:

i := new(int)

To access the value pointed to by a pointer, you can use the * operator:

name := "test"
p := &name
original := *p

The default value of a pointer is nil. This means that if an assignment is omitted, the pointer will contain a nil value, which can lead to a runtime panic if you attempt to dereference it.

Last updated on