Monday, 28 September 2020

golang 8 pointer reference

 package main

import "fmt"

func pointer_func(str *string) {
*str = "value changed by pointer"
}

func string_func(str string) string {
str = "value is unchanged, but new string is returned by string_func"
return str
}

func main() {
value := "old value"
string_value := string_func(value)
fmt.Println(value, "\n", string_value)
pointer_func(&value)
fmt.Println(value)
}

//cmd
C:\Users\bob\golang1>go run tutorial.go
old value
 value is unchanged, but new string is returned by string_func
value changed by pointer

reference:

No comments:

Post a Comment