Skip to content

Latest commit

 

History

History
62 lines (49 loc) · 2.35 KB

pointers.md

File metadata and controls

62 lines (49 loc) · 2.35 KB

Overview

  1. Key points about Pointers in golang

Key Concepts

  1. Pointer is an address (of a variable)
  2. & means address-of
  3. * means two things:
    1. (part of a type) *int means a pointer type with an int base type
    2. (unary operator) *myVar means value-at, meaning dereference myVar
      1. *nil creates a panic (dereferencing a nil pointer)
  4. all stack frames are independent/isolated (cannot access other stack frames)
  5. C++ has both References and Pointers (the difference)
    1. C++ discourages pointers
  6. Passing pointer is not always faster since golang needs to do escape analysis to decide where to allocate (stack or heap)

Func parameters

  1. function parameters pass by value (copy)
    1. compiler will copy arguments from caller stack frame to callee stack frame
    2. If the value is a pointer, callee can mutate value (at the address)
    3. Passing pointer trades immutability for less copying
  2. If func accepts interface ...
    1. (convenience) Caller can pass value if no receivers accept pointer (because passing address makes no difference)
    2. Otherwise, caller must pass address of the implementation
    3. Compiler & IDE enforces all of this
  3. Maps, slices, channels, pointers, functions pass "like" references
    1. They are passing address by-value

Func return parameters

  1. returned parameters pass by value (copy)
    1. compiler will copy return values from callee stack frame to caller stack frame
    2. If the return value is a pointer, compiler will allocate on heap

Pointer func args

func acceptValue(p int) {
    // changes have zero impact on caller
    p = 10
}

func acceptPointer(p *int) {
    // mutate value at p, affects caller
    *p = 10

    // CANNOT make caller's pointers point somewhere else
}

Idioms

  1. Accept pointer for mutation & REALLY large objects
  2. Accept non-pointer for immutability

Other Resources

  1. https://go.dev/ref/spec#Calls
  2. https://go.dev/tour/moretypes/1
  3. https://www.practical-go-lessons.com/chap-15-pointer-type
  4. https://gobyexample.com/pointers
  5. https://www.golang-book.com/books/intro/8