reflect.Addr() Function in Golang
Overview
Run-time reflection implementation support is integrated into the Go language, allowing programs to work with objects of any type with the aid of the reflect package. The mirror. Golang's Addr() function obtains the pointer value corresponding to the address. To use this function, one must import the reflect package into the program.
Syntax of reflect.Addr() Function in Golang
Syntax:
Return Value of reflect.Addr() Function in Golang
Return Value: This function returns the pointer value representing the address of v.
Exceptions of reflect.Addr() Function in Golang
As of my last knowledge update in September 2021, the reflect package in Go does not have a function named Addr(). It's possible that there have been updates or changes to the Go language or its standard library since then, so I recommend checking the latest Go documentation or source code to verify if such a function exists in the current version of Go.
How does the reflect.Addr() Function in Golang work?
Here's a working version of function f:
The conversion to integer pointer goes as follows:
- v is a reflect.Value representing the int field.
- v.Addr() is a relfect.Value representing a pointer to the int field.
- v.Addr().Interface() is an interface{} containing the int pointer.
- v.Addr().Interface().(*int) type asserts the interface{} to a *int
You can set the field directly without getting a pointer:
If you are passing the value along to something expecting interface{} (like the db/sql Scan methods), then you can remove the type assertion:
Examples
The following examples show how to use the previously mentioned approach in Golang:
Example 1:
Output:
Explanation:
This Go code defines a Person struct with Height and Age fields, each tagged for JSON serialization. It uses reflection to create an instance of Person, sets values for its fields (0.4 for Height and 2 for Age), and obtains a pointer to the struct. Finally, it prints the populated Person struct, demonstrating using the reflect package for dynamic struct creation and manipulation.
Example 2:
Output:
Explanation:
The code defines a Go program that demonstrates reflection in Go. It starts by defining a SuperInt struct and an interface A with a method LOL. It creates an instance of SuperInt, accesses one of its fields directly, and then uses reflection to access the same field through a pointer to the instance. The program prints the field's value directly and through reflection to showcase how reflection can work dynamically with struct fields.
Conclusion
The reflect.Addr() function in Go's reflect package is not a standalone function but rather an operation performed on a reflect.Value. It returns a new reflect.Value representing the address of the underlying value, allowing indirect access to it. This is commonly used to modify values within a struct or interface when working with reflection. However, it's important to use it carefully, as improper use can lead to runtime panics or undefined behavior. It's a powerful tool for dynamic manipulation but should be used judiciously and with a clear understanding of Go's type system.