This question already has an answer here:
I pulled this off:
package main
import "fmt"
type test struct {
A *int
B string
}
func main() {
x := 1
var A test
A.B = "hello"
A.A = &x
fmt.Printf("%s, %v", A.B, *A.A)
}
Playground: https://play.golang.org/p/iMsFBTWkRJU
I know using x:=1
and A.A = &x
is over doing it. How can I modify and make it simpler?
Thanks for your time
</div>
For example,
package main
import "fmt"
type T struct {
A *int
B string
}
func newT(a int, b string) *T {
return &T{A: &a, B: b}
}
func main() {
t := newT(1, "Hello")
fmt.Printf("%s, %v", t.B, *t.A)
}
Output:
Hello, 1