reflect.Addr() Function in Golang

Learn via video courses
Topics Covered

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

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:

Sharpen Your Fundamentals with Free Learning

Return Value of reflect.Addr() Function in Golang

Return Value: This function returns the pointer value representing the address of v.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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:

  1. v is a reflect.Value representing the int field.
  2. v.Addr() is a relfect.Value representing a pointer to the int field.
  3. v.Addr().Interface() is an interface{} containing the int pointer.
  4. 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:

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.

See Also