Aim: understanding the difference between *string
and string
in Golang
Attempt
func passArguments() {
username := flag.String("user", "root", "Username for this server")
flag.Parse()
fmt.Printf("Your username is %q.", *username)
fmt.Printf("Your username is %q.", username)
}
results in:
Your username is "root".Your username is %!q(*string=0xc820072200)
but when the *string is assigned to a string:
bla:=*username
fmt.Printf("Your username is %q.", bla)
it is able to print the string again:
Your username is "root".Your username is %!q(*string=0xc8200781f0).Your username is "root".
Questions
- Why is a *string != string, e.g. display of:
"root"
vs.%!q(*string=0xc8200781f0)
? - In what other cases should a *string be used instead of a string and why?
- Why is it possible to assign a
*string to a string variable, while the display of the string is different, e.g. display of:
"root"
vs.%!q(*string=0xc8200781f0)
?