Home > Backend Development > Golang > Set the values ​​of map1 and map2 through reflector pointers

Set the values ​​of map1 and map2 through reflector pointers

PHPz
Release: 2024-02-09 18:50:08
forward
531 people have browsed it

Set the values ​​of map1 and map2 through reflector pointers

php editor Youzi is here to introduce a method of using reflector pointers to set the values ​​of map1 and map2. Through the reflector pointer, we can easily access and modify the properties and methods of the object to dynamically set the values ​​of map1 and map2. This approach is not only simple and easy to understand, but also provides the flexibility to change the values ​​of map1 and map2 at any time as needed. Let’s take a look at the specific steps below!

Question content

Test my sample code: https://go.dev/play/p/g7lxjdnjnan

Final Results:

&{0}
&{1}
Copy after login

How do I want the output from the example terminal

&{1}
&{1}
Copy after login

My code in the project:

func Set(recv *Value, value Value) {
    reflect.ValueOf(recv).Elem().Set(reflect.ValueOf(value))
}

func (this *SetBlock) On_Exec(locals map[string]Value) {
        //this.inputs["Recv"].Value is pointing to the same value as global
    if this.inputs["Recv"].Value != nil {
        Set(&this.inputs["Recv"].Value, this.inputs["Value"].Value.Copy())
    }
        //this.layer.Game.Globals() is a original
        fmt.Println(this.layer.Game.Globals())
    this.outputs["After"].Value = Create_Bool(true)
}
Copy after login

I'm here because I don't even know how to research this.

Change a and b. The person who helped me use the Settings feature sent me a code. Hope you send it too. For some reason it worked, but now it doesn't.

I cannot assign to map, Because the way this function is made I don't even know if the value is coming from an array (if it's a variable or a map label)

The "Settings" function is available for different types https://go.dev/play/p/tggxpxf5kx9

One way I'm sure would work is to create a pointer, not a map pointer, A pointer to 2 mapped values

The "set" function is based on setting a pointer to another pointer. I think the error was that I was pointing to the wrong pointer

Workaround

I envisioned that when creating the value I created two pointers to the value, i.e. ** And change in this way. Thank you: @JimB

The above is the detailed content of Set the values ​​of map1 and map2 through reflector pointers. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template