Go基础_1_36 函数的值传递-引用传递

Tutorial: Go初级 Category: Go Published: 2026-04-07 13:58:26 Views: 21 Likes: 0 Comments: 0
package main

import "fmt"

func fun1(num int) { // 值传递:num = a = 10
    fmt.Println("fun1()函数中,num的值:", num)
    num = 100
    fmt.Println("fun1()函数中修改num:", num)
}

func fun2(p1 *int) { //传递的是a的地址,就是引用传递
    fmt.Println("fun2()函数中,p1:", *p1)
    *p1 = 200
    fmt.Println("fun2()函数中,修改p1:", *p1)
}

func fun3(arr2 [4]int) { // 值传递
    fmt.Println("fun3()函数中数组的:", arr2)
    arr2[0] = 100
    fmt.Println("fun3()函数中修改数组:", arr2)
}

func fun4(p2 *[4]int) { // 引用传递
    fmt.Println("fun4()函数中的数组指针:", p2)
    p2[0] = 200
    fmt.Println("fun4()函数中的数组指针:", p2)
}

func main() {
    /*
        指针作为参数:

        参数的传递:值传递,引用传递
    */

    a := 10
    fmt.Println("fun1()函数调用前,a:", a) // 10
    fun1(a)                          // 值传递,函数中修改a
    fmt.Println("fun1()函数调用后,a:", a) // 10, 未改变

    fun2(&a)
    fmt.Println("fun2()函数调用后,a:", a) // 200

    arr1 := [4]int{1, 2, 3, 4}
    fmt.Println("fun3()函数调用前:", arr1) // [1 2 3 4]
    fun3(arr1)
    fmt.Println("fun3()函数调用后:", arr1) // [1 2 3 4]

    fun4(&arr1)
    fmt.Println("fun4()函数调用后:", arr1) // [200 2 3 4]

}