Home >Backend Development >Golang >How Do I Get and Store the Address of a Go Struct Variable?

How Do I Get and Store the Address of a Go Struct Variable?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-15 20:00:25630browse

How Do I Get and Store the Address of a Go Struct Variable?

Obtaining and Storing the Address of a Struct Variable in Go

In Go, a struct variable's address can be obtained using the '&' operator. However, the default printing format in fmt.Println() treats struct addresses specially and prints them using '&{}' syntax.

To print the address of a struct variable explicitly, use the following code:

fmt.Printf("%p\n", &r)

This will output the memory address of the struct variable 'r'.

To store the address of a struct variable in a separate variable, use the following code:

addr := &r

The 'addr' variable will now contain the address of the struct variable 'r'.

The above is the detailed content of How Do I Get and Store the Address of a Go Struct Variable?. 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