Language is 'Go' ('Golang').
The Function initApp
is receiving a Pointer to an Object ('struct' in Go).
Inside the Function I create a new Object Pointer and initialize the Value of the Object. Printing and Debugger both show that before the Function's Return everything is good.
But after the Return, the Pointer which was the Function's Argument has the same empty Value as before the Function Call.
Why is that so?
Code is here: https://pastebin.com/0Gww2CQC
// ptr.go.
package main
import "fmt"
type ClassX struct {
Name string
Age int
}
func main() {
var obj *ClassX
initApp(obj)
fmt.Println(obj)
return
}
func initApp(x *ClassX) {
tmp := NewClassXObject()
x = tmp
fmt.Println("tmp:", tmp)
fmt.Println("x:", x)
}
func NewClassXObject() *ClassX {
x := new(ClassX)
x.init()
return x
}
func (o *ClassX) init() {
o.Age = 123
o.Name = "John"
}
The Output is following:
tmp: &{John 123}
x: &{John 123}
<nil>
Thank You!