Pointers
They pass the reference to value rather than the value itself, this helps to passing a copy of the value rather than the value itself.
// simple example to demonstrate pointer usage
package main
import (
"fmt"
)
func main() {
// a variable containing data
a := 45
// assign it through pointer
// this b is not pointing to a but it takes mem address of block memory
// where 45 is stored.
b := &a // now both has access to share space of mem
*b = 56 // assign another value to b
fmt.Println(a, *b) // print a and b
// (note we did not use * with a because it was assigned a value initially
// but b was referenced
c := a // Just assign a to another var c
// here it will pass the value and not address
c = 34
fmt.Println(a, c) // you will notice no change in a
}Last updated