Skip to content

Latest commit

 

History

History
 
 

pointers

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Pointers

Pointers provide a way to share data across function boundaries. Having the ability to share and reference data with a pointer provides flexbility. It also helps our programs minimize the amount of memory they need and can add some extra performance.

Notes

  • 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".

1.5 Garbage Collection

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.

figure1

Links

Pointer Mechanics

https://golang.org/doc/effective_go.html#pointers_vs_values
http://www.goinggo.net/2013/07/understanding-pointers-and-memory.html
http://www.goinggo.net/2014/12/using-pointers-in-go.html

Stacks

Contiguous Stack Proposal

Escape Analysis and Inlining

Go Escape Analysis Flaws
https://github.com/golang/go/wiki/CompilerOptimizations

Garbage Collection

https://en.wikipedia.org/wiki/Tracing_garbage_collection
https://blog.golang.org/go15gc
Rick Hudson GC Talk

Single Static Assignment Optimizations

GopherCon 2015: Ben Johnson - Static Code Analysis Using SSA
https://github.com/golang/go/blob/dev.ssa/src/cmd/compile/internal/ssa/compile.go#L83
https://godoc.org/golang.org/x/tools/go/ssa
Understanding Compiler Optimization

Code Review

Pass by Value (Go Playground)
Sharing data I (Go Playground)
Sharing data II (Go Playground)
Stack vs Heap (Go Playground)
Stack grow (Go Playground)

Exercises

Exercise 1

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)

Exercise 2

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.