Home > Backend Development > Golang > Golang pointer confusion, how to get the pointer from the function and then pass it to the modifying function

Golang pointer confusion, how to get the pointer from the function and then pass it to the modifying function

WBOY
Release: 2024-02-06 10:39:03
forward
590 people have browsed it

Golang 指针混淆,如何从函数获取指针,然后传递给修改函数

Question content

I want to use a library (golang walk declarative) which wants me to pass a pointer variable and the library will use an instance later Fill it.

For bookkeeping purposes I tried creating a function to return the reference and passing it further, but in the original function I did not get back the reference to the correct object.

I tried to simplify the problem but I still can't figure it out correctly, how to populate the value world into a map inside the test_ref structure without modifying the setup function.

Working code

<code>var t *walk.LineEdit
...
                    LineEdit{
                        AssignTo: &t,
                    },
</code>
Copy after login

My attempt

LineEdit{
                        AssignTo: GetLineEdit("msg"),
                    },
...
func GetLineEdit(name string) **walk.LineEdit {
Copy after login

Test code

type test_ref struct {
    v map[string]*string
}

func (t *test_ref) init() {
    t.v = map[string]*string{}
}

func (t *test_ref) return_ref() **string {
    s := "hello"
    t.v["a"] = &s
    p := t.v["a"]
    return &p
}

type test_setup struct {
    s **string
}

//dont modify this function
func setup(t test_setup) {
    w := "world"
    *(t.s) = &w
}

func main() {
    tr := test_ref{}
    tr.init()
    s := tr.return_ref()
    setup(test_setup{
        s: s,
    })
    fmt.Println(*tr.v["a"])//logging hello

}
Copy after login

If I make a small modification to the setup function, I can get it to work, but since I don't want to update the walking library, I was wondering if there is a way to do this without touching the setup function. < /p>

<code>func setup(t test_setup) {
    w := "world"
    **(t.s) = w
}
</code>
Copy after login


Correct answer


Here:

func (t *test_ref) return_ref() **string {
    s := "hello"
    t.v["a"] = &s
    p := t.v["a"]
    return &p
}
Copy after login

What you returned is the address of variable p.

I think this is what you are trying to do:

func (t *test_ref) return_ref() *string {
    s := "hello"
    t.v["a"] = &s
    return &s
}
Copy after login

The above will return the address of s, which is what is stored in the map. Then:

Chapter 823

This will set the value of the string to "world".

You can further your education and do the following:

type test_ref struct {
    v map[string]**string
}

func (t *test_ref) init() {
    t.v = map[string]**string{}
}

func (t *test_ref) return_ref() **string {
    s := "hello"
    k := &s
    t.v["a"] = &k
    return &k
}

type test_setup struct {
    s **string
}

// dont modify this function
func setup(t test_setup) {
    w := "world"
    *(t.s) = &w
}

func main() {
    tr := test_ref{}
    tr.init()
    s := tr.return_ref()
    setup(test_setup{
        s: s,
    })
    fmt.Println(**tr.v["a"]) //logging hello

}
Copy after login

The above is the detailed content of Golang pointer confusion, how to get the pointer from the function and then pass it to the modifying function. 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