SoFunction
Updated on 2025-04-07

The use and difference between Golang's reference type and pointer

In Golang, reference types and pointers are two easily confused concepts, but they have essential differences. Understanding their differences is essential for writing efficient, correct Go code.

1. Reference Type

Reference types are a general term for some built-in types in Go, whose values ​​share the underlying data when passed, rather than copying the data. Reference types in Go include:

  • Slice
  • Map (map)
  • Channel
  • Function (function)
  • Interface

Features:

  • Sharing the underlying data: When passing a reference type, a reference to the underlying data is passed, not a copy of the data.
  • No explicit dereference is required: you can modify the underlying data by directly manipulating the reference type.
  • The value of zero isnil: The zero value of the reference type isnil, means that it is not initialized.

Example:

func modifySlice(s []int) {
    s[0] = 100 // Modify the underlying data}

func main() {
    s := []int{1, 2, 3}
    modifySlice(s)
    (s) // Output: [100, 2, 3]}
  • sis a slice (reference type), passed tomodifySliceWhen sharing the underlying array, the modification will be reflected in the original slice.

2. Pointer

A pointer is a variable that stores the memory address of another variable. The value of the target variable can be accessed or modified indirectly through a pointer.

Features:

  • Explicit dereference: Need to pass*The operator accesses the value of the target variable.
  • The value of zero isnil: The uninitialized pointer value isnil
  • Passing address: The pointer passes the address of the variable, not the value itself.

Example:

func modifyInt(p *int) {
    *p = 100 // Dereference and modify the value of the target variable}

func main() {
    x := 10
    modifyInt(&x) // Pass the address of x    (x) // Output: 100}
  • pIt's a pointer, it's storedxThe address of*pRevisexvalue.

3. Reference type vs pointer

characteristic Reference Type pointer
nature Built-in types (such as slices, maps) Variables that store variable addresses
Transmission method Sharing the underlying data The address of the variable is passed
Decision No explicit dereference required Need explicit dereference (*Operator)
Zero value nil nil
Applicable scenarios Scenarios that need to share underlying data (such as slicing, mapping) Scenarios that need to be directly modified to change the value

4. Common misunderstandings

Myth 1: Slices are pointers

  • Misunderstanding: Slices are reference types, but they are themselves a structure (including pointers, lengths, and capacity to the underlying array), not pointers.
  • Correct understanding: the underlying array is shared when the slice is passed, but the slice itself is passed by value (the structure is copied).

Myth 2: Reference type does not require pointers

  • Misunderstanding: The reference type already shares the underlying data, so no pointers are needed.
  • Correct understanding: Reference types share data when passed, but if you need to modify the reference type itself (such as reassigning slices), you need to use a pointer.

Example:

func reassignSlice(s *[]int) {
    *s = []int{4, 5, 6} // Modify the slice itself}

func main() {
    s := []int{1, 2, 3}
    reassignSlice(&s) // Pass the pointer to the slice    (s) // Output: [4, 5, 6]}
  • Here you need to pass the pointer to the slice because you need to modify the slice itself (reallocation).

5. Summary

  • Reference type: Share the underlying data, suitable for passing big data structures (such as slicing, mapping).
  • Pointer: Pass the variable address, suitable for scenarios where the value of the variable needs to be directly modified and changed.
  • Select by:
    • If you need to share data, use the reference type first.
    • If you need to modify the variable itself (such as reassignment), use a pointer.

Understanding the difference between the two can help write more efficient and clearer Go code.

This is the article about Golang's reference types and pointer usage. For more related Golang's reference types and pointer content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!