Home >Backend Development >Golang >What's the Difference Between Value and Pointer Semantics in Go?

What's the Difference Between Value and Pointer Semantics in Go?

DDD
DDDOriginal
2024-12-22 09:31:34240browse

What's the Difference Between Value and Pointer Semantics in Go?

Understanding Value and Pointer Semantics in Go

In Go, when passing values to functions or methods, we often encounter the concepts of value and pointer semantics. These concepts play a crucial role in understanding how data is handled and modified within programs.

Value Semantics

Value semantics implies that when we pass a value of a variable as an argument to a function, a copy of that value is created and passed. The function can only operate on and modify the copy, not the original variable.

func double(i int) int {
    i *= 2
    return i
}

func main() {
    i := 1
    fmt.Println("Original i:", i)  // 1
    fmt.Println("Doubled i:", double(i))  // 2
    fmt.Println("Updated i:", i)  // 1
}

In this example, the value of i is copied into the double function, and the function modifies the copy. The original value of i remains unchanged.

Pointer Semantics

In contrast to value semantics, pointer semantics means that when we pass a pointer to a variable as an argument to a function, the function can directly modify the original variable.

func doublePointer(ptr *int) {
    *ptr *= 2
}

func main() {
    i := 1
    fmt.Println("Original i:", i)  // 1
    fmt.Println("Doubled i:", doublePointer(&i))  // 2
    fmt.Println("Updated i:", i)  // 2
}

Here, we pass the address of i to the doublePointer function using a pointer (*). The function can then modify the value at the address, and the original variable i is updated.

Slices: A Case of Pointer Semantics

An interesting aspect of Go is that while slices are passed by value, they exhibit pointer semantics. This means that if we pass a slice to a function, the function can modify the elements of the underlying array, which are also referenced by the original slice.

func doubleSlice(s []int) {
    for i := range s {
        s[i] *= 2
    }
}

func main() {
    is := []int{1, 2}
    fmt.Println("Original slice:", is)  // [1, 2]
    fmt.Println("Doubled slice:", doubleSlice(is))  // [2, 4]
    fmt.Println("Updated slice:", is)  // [2, 4]
}

In this example, even though we pass is as a value, the doubleSlice function can modify its elements because slices store a reference to their underlying array. Therefore, the elements of the original slice are also updated.

Understanding value and pointer semantics is crucial for effective programming in Go. It allows developers to control how data is modified and shared within and between functions.

The above is the detailed content of What's the Difference Between Value and Pointer Semantics in Go?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn