Home >Backend Development >Golang >Go Pointers: What's the Difference Between `&` and `*` and How Are They Used?

Go Pointers: What's the Difference Between `&` and `*` and How Are They Used?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-30 18:40:11478browse

Go Pointers: What's the Difference Between `&` and `*` and How Are They Used?

& and * Pointers: Differences and Usage

In Go, function arguments can be passed either by value or by reference. When passing by value, a copy of the variable is created and passed to the function. When passing by reference, the address of the variable is passed instead, allowing the function to modify the original value.

This concept of passing by reference is achieved using pointers. A pointer is a variable that holds the address of another variable. The & operator returns the address of a variable, while the * operator is used to access the value stored at an address.

Using &

The & operator is used to pass variables by reference as function arguments. For example, consider the following function:

func SendNotification(user *User) {
    // ...
}

This function expects a *User as an argument, where * indicates that the argument is a pointer to a User struct. To pass a variable u of type User, it is necessary to use the & operator like so:

SendNotification(&u)

This passes the address of u to the function, allowing it to modify the original value.

Using *

The * operator is used to access the value stored at an address. For example, given the following code:

var userPtr *User
userPtr = &u

userPtr is a pointer that holds the address of the User struct stored in u. To access the value stored at this address, we can use the * operator:

*userPtr = User{name: "John"}

This assigns a new value to the User struct at the address stored in userPtr.

Distinction and Usage

The distinction between & and * lies in their functionality. & is used to obtain the address of a variable, allowing it to be passed by reference. * is used to access the value stored at an address, allowing for modifications to the original value.

In practice, & is commonly used to pass variables by reference to functions, while * is used to modify the value stored at an address, such as when using pointers as receivers in methods.

Example Usage

Consider the following code:

func main() {
    u := User{name: "Jane"}

    SendNotification(&u) // Passing by reference

    fmt.Println(u.name) // Prints "Jane"
}

Here, the SendNotification function takes a *User as an argument. By passing &u, we allow the function to modify the original value of u. Inside the function, the value could be modified like so:

func SendNotification(user *User) {
    user.name = "John"
}

This would change the name of the User struct that was passed by reference.

The above is the detailed content of Go Pointers: What's the Difference Between `&` and `*` and How Are They Used?. 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