¡GoConqr estrena diseño! Cuéntanos qué te parece.

Mapa mental

Pointers

Filipi Melo Learning Go Book Inglés

Pointers

Contenido del mapa

Pointers

  1. 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
    • & Operator
      • Address operator
        • Returns the memory address of a value
          • Example: pointerToX := &x
    • * 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.
  2. 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
  3. Pointers Are a Last Resort

    • Caution with Pointers
      • Harder to track data flow
      • Increases garbage collector workload.
  4. The Zero Value Versus No Value

    • nil Pointers
      • Useful for distinguishing between zero-value assignment and no assignment.
  5. 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.
    • 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.
  6. 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.

Imágenes y archivos del mapa

  • Pointers
  • Pointers
  • Pointers
  • Pointers
  • Pointers
  • Pointers
  • Pointers
  • Pointers
  • Pointers

Información

Descripción
Book (CH6: Pointers) Learning Go Mapa Mental sobre Pointers, creado por Filipi Melo el 23/10/2024.
Creado por
Filipi Melo · oct 2024
Actualizado
23 oct 2024
Visitas
0
Valoración

Más sobre Go Programming

Escríbenos

¿Sobre qué es tu mensaje?

¿Qué te parece el nuevo diseño?

¿Te gusta?