package mainimport "fmt"func main() {s1 := "hello"s2 := "world"s3 := name(s1, s2)fmt.Println(s3) //println(name)会出错,所以用s3把name的返回值存下来打印fmt.Println(s1)}func name (v1 string, v2 string) string { //定义函数时返回值也要声明类型v1 = v1 + "_"fmt.Println(v1)return v1 + v2}
hello_ //v1 valuehello_world //s3 valuehello //s1 value
go 中传参时是复制一份 然后把副本传入。所以 name() 中的 v1 + “_” 并没有对 s1 进行操作,而是对s1的副本做了操作。
