What is the point of passing a pointer to a strings in go (golang)?
go, pointers, string
Solution
You pass a pointer to the "object" holding the string so that you can assign a different string to it.
Example: http://play.golang.org/p/Gsybc7Me-5
func ps(s *string) {
*s = "hoo"
}
func main() {
s := "boo"
ps(&s)
fmt.Println(s)
}
Problem
I was reading the following conversation about go (golang) strings. Strings in go are just a pointer to a (read-only) array and a length. Thus, when you pass them to a function the pointers are passed as value instead of the whole string. Therefore, it occurred to me, if that is true, then why are you even allowed to define as a function with a signature that takes `*string` as an argument? If the string is already doing plus, the data is immutable/read-only, so you can't change it anyway. What is the point in allowing go to pass pointers to strings if it already does that internally anyway?