go - String pointers -
consider following program (http://play.golang.org/p/ibastvudte):
package main import ( "fmt" ) func changestringvaluenotok(dest *string, src string) { dest = &src } func changestringvalueok(dest *string, src string) { *dest = src } func main() { := "hello" b := "world" changestringvaluenotok(&a, b) fmt.println(a) // still "hello" changestringvalueok(&a, b) fmt.println(a) // "world" }
my goal call function , change value of string. works fine second function, not first.
question: meaning of *dest = src
compared dest = &src
? guess former "the contents of dest src" , latter "change dest variable points address of src" discards previous value, not contents of a. if right, don't understand how *dest = src
works.
i hope question isn't fuzzy.
*dest = src
is: set value pointed @ dest
value in src
. it's effective.
dest = &src
is: set value of dest
address of src
. dest formal parameter of changestringvaluenotok
change (to pointer only, not pointee) visible locally. changed value not used, it's total effect no operation.
Comments
Post a Comment