Home  >  Article  >  Backend Development  >  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
WBOYforward
2024-02-06 10:39:03542browse

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>

My attempt

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

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

}

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.

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


Correct answer


Here:

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

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
}

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

}

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete