Pointers provide a way to share data across program boundaries. Having the ability to share and reference data with a pointer provides the benefit of efficiency. There is only one copy of the data and everyone can see it changing. The cost is that anyone can change the data which can cause side effects in running programs.
- Use pointers to share data.
- Values in Go are always pass by value.
- "Value of", what's in the box. "Address of" ( & ), where is the box.
- The (*) operator declares a pointer variable and the "Value that the pointer points to".
- When a value could be referenced after the function that constructs the value returns.
- When the compiler determines a value is too large to fit on the stack.
- When the compiler doesn’t know the size of a value at compile time.
- When a value is decoupled through the use of function or interface values.
The design of the Go GC has changed over the years:
- Go 1.0, Stop the world mark sweep collector based heavily on tcmalloc.
- Go 1.2, Precise collector, wouldn't mistake big numbers (or big strings of text) for pointers.
- Go 1.3, Fully precise tracking of all stack values.
- Go 1.4, Mark and sweep now parallel, but still stop the world.
- Go 1.5, New GC design, focusing on latency over throughput.
- Go 1.6, GC improvements, handling larger heaps with lower latency.
- Go 1.7, GC improvements, handling larger number of idle goroutines, substantial stack size fluctuation, or large package-level variables.
- Go 1.8, GC improvements, collection pauses should be significantly shorter than they were in Go 1.7, usually under 100 microseconds and often as low as 10 microseconds.
- Go 1.9, Large object allocation performance is significantly improved in applications using large (>50GB) heaps containing many large objects.
- Go 1.10, Many applications should experience significantly lower allocation latency and overall performance overhead when the garbage collector is active.
Garbage Collection Semantics Part I - William Kennedy
_"The stack is for data that needs to persist only for the lifetime of the function that constructs it, and is reclaimed without any cost when the function exits. The heap is for data that needs to persist after the function that constructs it exits, and is reclaimed by a sometimes costly garbage collection." - Ayan George
Pointers vs. Values
Language Mechanics On Stacks And Pointers - William Kennedy
Using Pointers In Go - William Kennedy
Understanding Pointers and Memory Allocation - William Kennedy
Go Escape Analysis Flaws
Compiler Optimizations
The Garbage Collection Handbook
GC Pacer Redesign - 2021 - Michael Knyszek
Tracing Garbage Collection
Go Blog - 1.5 GC
Go GC: Solving the Latency Problem
Concurrent garbage collection
Go 1.5 concurrent garbage collector pacing
Eliminating Stack Re-Scanning
Why golang garbage-collector not implement Generational and Compact gc? - Ian Lance Taylor
Getting to Go: The Journey of Go's Garbage Collector - Rick Hudson
Garbage Collection In Go : Part I - Semantics - William Kennedy
Garbage Collection In Go : Part II - GC Traces - William Kennedy
Garbage Collection In Go : Part III - GC Pacing - William Kennedy
Go memory ballast: How I learnt to stop worrying and love the heap - Ross Engers
GopherCon 2015: Ben Johnson - Static Code Analysis Using SSA
package ssa
Understanding Compiler Optimization
Debugging code generation in Go - JBD
Pass by Value (Go Playground)
Sharing data I (Go Playground)
Sharing data II (Go Playground)
Escape Analysis (Go Playground)
Stack grow (Go Playground)
Indirect Assignment
Indirection Execution
Assignment Slices Maps
Indirection Level Interfaces
Unknown
Part A Declare and initialize a variable of type int with the value of 20. Display the address of and value of the variable.
Part B Declare and initialize a pointer variable of type int that points to the last variable you just created. Display the address of , value of and the value that the pointer points to.
Template (Go Playground) | Answer (Go Playground)
Declare a struct type and create a value of this type. Declare a function that can change the value of some field in this struct type. Display the value before and after the call to your function.
Template (Go Playground) | Answer (Go Playground)
All material is licensed under the Apache License Version 2.0, January 2004.