Mind map
Pointers

Map content
Pointers
A Quick Pointer Primer
- Pointer
- Variable holding the memory location (address) of another variable.
- Memory Address
- Always occupy the same amount of memory.
- nil
- Zero value for a pointer, indicating no value.
- Unlike NULL in C, nil is not interchangeable with 0.
- Defined in the universe block
- nil can be shadowed so it should not be used as a variable name
- Zero value for a pointer, indicating no value.
- & Operator
- Address operator
- Returns the memory address of a value
- Example: pointerToX := &x
- Returns the memory address of a value
- Address operator
- * Operator
- Example: fmt.Println(*pointerToX) // prints the value pointed to by pointerToX.
- Indirection/dereference operator
- Returns the value stored at a pointer’s address.
- Pointer Type
- A type that represents a pointer
- Denoted by * before a type name
- Can be based on any type
- new Function
- Creates a pointer to a zero-value instance of a type.
- Addressing Constants
- Use helper functions to turn constants into pointers.
- Pointer
Pointers Indicate Mutable Parameters
- Immutability
- Go's choice between value and pointer parameters addresses the need for immutability.
- Easier to understand, safer, and better for enforcing contracts.
- Call-by-Value
- Go passes copies of values to functions.
- Non-pointer types
- Original data is immutable in the called function.
- Pointer type
- Function can modify the original data.
- nil Pointer in Functions
- Can’t make nil non-nil
- Only reassign if a value already exists.
- Pointer Modification in Functions
- Changing the pointer changes the copy, not the original.
- To retain changes, dereference the pointer and set the value
- Changing the pointer changes the copy, not the original.
- Can’t make nil non-nil
- Immutability
Pointers Are a Last Resort
- Caution with Pointers
- Harder to track data flow
- Increases garbage collector workload.
- Caution with Pointers
The Zero Value Versus No Value
- nil Pointers
- Useful for distinguishing between zero-value assignment and no assignment.
- nil Pointers
The Difference Between Maps and Slices
- Maps
- Implemented as pointers to structs; passing a map copies the pointer.
- Avoid maps in public APIs
- Lack of explicit key definitions and poor immutability.
- Use structs instead of maps
- Better type safety and self-documentation
- Maps are ideal when keys aren’t known at compile time.
- Avoid maps in public APIs
- Implemented as pointers to structs; passing a map copies the pointer.
- Slices
- Modifying slice contents affects the original, but append may not.
- Appending with enough capacity modifies shared memory
- Otherwise, a new memory block is created.
- Slice can be modified in content but not resized.
- Documentation should specify if a function modifies a slice’s contents.
- A slice is implemented as a struct with three fields: an int field for length, an int field for capacity, and a pointer to a block of memory.
- Slices as Buffers
- Reuse slices to minimize allocations, improving memory efficiency.
- Maps
Reducing the Garbage Collector’s Workload
- Garbage
- Data without pointers, reclaimable by the garbage collector
- Minimize unnecessary allocations to reduce garbage and workload on the garbage collector.
- Garbage
Images and files in the map
Information
- Description
- Book Learning Go (CH6: Pointers) Mind Map on Pointers, created by Filipi Melo on 23/10/2024.
- Created by
- Filipi Melo · Oct 2024
- Updated
- Oct 23, 2024
- Views
- 0
- Rating







