1. package main
    2. import "fmt"
    3. func main() {
    4. s1 := "hello"
    5. s2 := "world"
    6. s3 := name(s1, s2)
    7. fmt.Println(s3) //println(name)会出错,所以用s3把name的返回值存下来打印
    8. fmt.Println(s1)
    9. }
    10. func name (v1 string, v2 string) string { //定义函数时返回值也要声明类型
    11. v1 = v1 + "_"
    12. fmt.Println(v1)
    13. return v1 + v2
    14. }
    1. hello_ //v1 value
    2. hello_world //s3 value
    3. hello //s1 value

    go 中传参时是复制一份 然后把副本传入。所以 name() 中的 v1 + “_” 并没有对 s1 进行操作,而是对s1的副本做了操作。