How to Pass Nested Structures By Reference in Reflection Using Value.Addr()?

Linda Hamilton
Release: 2024-10-24 03:34:02
Original
685 people have browsed it

How to Pass Nested Structures By Reference in Reflection Using Value.Addr()?

Nested Structures and Pass-by-Reference in Reflection

In Go, understanding nested structures and how to pass them by reference in reflection is crucial. Consider a scenario where you have the nested Client and Contact structures:

<code class="go">type Client struct {
    Id                int
    Age               int
    PrimaryContact    Contact
    Name              string
}

type Contact struct {
    Id        int
    ClientId  int
    IsPrimary bool
    Email     string
}</code>
Copy after login

When you introspect the PrimaryContact field of the Client struct, you may encounter a "reflect.Value.Set using unaddressable value" panic. This is because PrimaryContact is passed by value, not by reference. To resolve this issue, we need to pass PrimaryContact by reference using reflection.

Solution Using Value.Addr()

  1. Pass the pointer to the Client struct: To set field values of a struct using reflection, you must pass it as a pointer. In this case, &client is the pointer to the Client struct.
  2. Get pointer value of the PrimaryContact field: Use Value.Addr() to get the pointer value of the PrimaryContact field. This addressable value can be used for setting the field values of the nested struct.

Code:

<code class="go">package main

import (
    "fmt"
    "reflect"
)

type Client struct {
    Id                int
    Age               int
    PrimaryContact    Contact
    Name              string
}

type Contact struct {
    Id        int
    ClientId  int
    IsPrimary bool
    Email     string
}

func main() {
    client := Client{}

    v := reflect.ValueOf(&client)
    primaryContact := v.FieldByName("PrimaryContact").Addr()

    primaryContact.FieldByName("Id").SetInt(123)
    primaryContact.FieldByName("ClientId").SetInt(456)
    primaryContact.FieldByName("IsPrimary").SetBool(true)
    primaryContact.FieldByName("Email").SetString("example@example.com")

    fmt.Printf("%+v\n", client)
}</code>
Copy after login

Output:

{Id:0 Age:0 PrimaryContact:{Id:123 ClientId:456 IsPrimary:true Email:example@example.com} Name:}
Copy after login

Key Points:

  • To pass a nested struct by reference, use reflect.Value.Addr().
  • Set field values using reflect.Value.SetInt(), reflect.Value.SetString(), etc.
  • Iterate over the struct fields to set values for all nested structures.

The above is the detailed content of How to Pass Nested Structures By Reference in Reflection Using Value.Addr()?. For more information, please follow other related articles on the PHP Chinese website!

source:php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!