Pointers

Let's learn about Go's pointers

Go allows you to do low-level programming with high-level code

Check memory address and access that address

: Can be used when you don't want values to be copied

ex)

package main

import "fmt"

func main() {
 a := 2
 b := a
 a = 10
  fmt.Println(a, b) // -> 10 2
}
  • In the above example, you can see that the value is copied

    • That is, even if you update the value of a, the value of b does not change

However, when you want a memory address instead of copying the value, you can use the method below

& operator

: You can use & to find the address of memory

Accessing memory address using & operator

  • Here, by doing b := &a, b is looking at the memory of a

    • That's why the same memory address is output as above

  • b is directly called a pointer

* operator

: You can use ***** and addresses to look at the value of that memory address

  • When dealing with very heavy data structures, using addresses stored in memory is efficient since you don't keep making copies

    • By utilizing pointers like this, you can make programs run fast!

Changing values through pointers

  • You can change the value of a using pointer b that is connected to a's address

Last updated