code:
type account struct { owner string balance int } func NewAccount(owner string) *account { account := account{owner: owner, balance: 0} return &account }
question: Why NewAccount return value is &{name, 0} why not a memory address ?
That's the default fmt print format. See the section on verbs to understand how the change the format: https://golang.org/pkg/fmt/#hdr-Printing
fmt
fmt.Println(NewAccount("name")) // output: &{name 0}
fmt.Printf("%p ", NewAccount("name")) // output: 0xc00010c040
https://play.golang.org/p/I-EC_1yTqT-
Alternatively use the builtin print or println functions, but keep in mind that these are not guaranteed to stay in the language.
print
println
println(NewAccount("name")) // output: 0xc0000a0f60
https://play.golang.org/p/bH97NGStYsi
2.1m questions
2.1m answers
60 comments
57.0k users