SoFunction
Updated on 2025-03-05

Use of go value assignment and reference assignment

Value Assignment

Value assignment refers to copying the value of one variable to another variable. When a value assignment is performed, a copy of the original value is created and the copy is assigned to the target variable. This means that the target variable and the source variable are completely independent, and they refer to different memory addresses. Modifying the value of the target variable will not affect the value of the source variable.

var a = 10
var b = a // Value assignmentb = 20
(a) // Output: 10(b) // Output: 20

In the above code, put the variableaAssign the value to the variableb, and then modifybThe value of   will not affecta

Reference Assignment

Reference assignment refers to copying a reference (pointer, slice, map, channel, etc.) to another variable. When making reference assignments, the target variable and the source variable refer to the same underlying data. They point to the same memory address. Therefore, modifying the target variables affects the source variables because they share the same data.

var slice1 = []int{1, 2, 3}
var slice2 = slice1 // Reference assignmentslice2[1] = 10
(slice1) // Output: [1 10 3](slice2) // Output: [1 10 3]

In the above code, sliceslice1The reference toslice2, and then modifyslice2The elements in it will affectslice1, because they refer to the same piece of memory.
It should be noted that the behavior of value assignment and reference assignment depends on the type of variable. For basic types (such as integers, floating point numbers, boolean values, etc.), the value will be copied when value assignment is performed; for reference types (such as slices, maps, channels, etc.), the underlying data will be shared when reference assignment is performed.

Structure value assignmentIn Go language, the assignment operation of the structure type is to copy the value

package main

import "fmt"

type Person struct {
	Name string
	Age  int
}

func main() {
	p1 := Person{Name: "Alice", Age: 30}

	p2 := p1

	p3 := Person{Name: , Age: }

	 = 31

	(p2) // Output: {Alice 30}	(p3) // Output: {Alice 30}}

When p2 := p1 is executed, a new structure variable p2 is created and the field values ​​of p1 are copied into p2 one by one.

A new structure variable p3 is created and the field values ​​of p1 are copied to p3 one by one. Therefore, p3 is an independent structure variable, and it points to a different memory space from p1. When modifying the fields of p1, p3 will not be affected because they refer to different memory addresses.

Reference assignment of structureReference assignment of a structure refers to assigning a reference (pointer) of one structure variable to another structure variable. By referencing assignment, both structure variables will reference the same underlying data.

Reference assignments are applicable to structure types, including custom structure types and built-in structure types (such as slices, maps, etc.).

type Person struct {
    Name string
    Age  int
}

func main() {
    p1 := Person{Name: "Alice", Age: 30}

    // Reference assignment    p2 := &p1

     = 31

    (p1) // Output: {Alice 31}    (*p2) // Output: {Alice 31}}

In the above code, the pointer of p1 is assigned to p2 through p2 := &p1, thereby realizing reference assignment. Both p2 and p1 point to the same structure data.

Therefore, when modifying the field value of p1, access through p2 will also reflect the modified result. This is because p2 and p1 refer to the same memory space, and they share the same data.

It should be noted that reference assignment is a shallow copy operation. This means that only the structure itself is copied, and the reference type fields (such as slices, maps, etc.) inside the structure will not be copied. When modifying the reference type field, it will affect all structure variables that reference the field.

type Person struct {
    Name string
    Friends []string
}

func main() {
    p1 := Person{Name: "Alice", Friends: []string{"Bob", "Charlie"}}

    // Reference assignment    p2 := &p1

    [0] = "David"

    (p1) // Output: {Alice [David Charlie]}    (*p2) // Output: {Alice [David Charlie]}}

In the above code, the pointer of p1 is assigned to p2 by reference assignment. When modifying elements of the Friends field in p1, access through p2 will also reflect the modification results. This is because p1 and p2 share references to the same slice, which point to the same underlying data.

Slice value assignmentcopy is a built-in function that copies elements of one slice into another

package main

import "fmt"

func main() {
	ls := []int{2, 45, 4, 57}
	var lk = make([]int, 5)
	copy(lk, ls)
	lk[1] = 2222
	(ls, lk)
}

The element of ls is copied to lk through the copy function and the value of lk[1] is modified. This modification will only affect lk itself and will not affect ls. Because the copy function will copy the element value of the source slice into the target slice. In this process, the source slice and the target slice are completely independent, referring to different underlying arrays respectively. Modifying the element values ​​in the target slice lk does not affect the original slice ls because they refer to different memory spaces.

This is the article about the use of go value assignment and citation assignment. For more related go value assignment and citation assignment, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!