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
Release: 2024-12-15 20:00:25
Original
631 people have browsed it

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)
Copy after login

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
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template