Golang在函数中修改外部变量

修改map结构体成员的问题

Go struct类型的map结构体成员不能修改的问题

值类型数据传地址的修改方法

func change(isT *bool) {
    *isT = true
}

func TestChange(t *testing.T) {
    isT := false
    change(&isT)
    fmt.Println("isT: ", isT) // true
}

结构体数据

type Student struct {
    Name  string
    Score float64
}

func changeStudent(stu *Student, newName string, newScore float64) {
    stu.Name = newName
    stu.Score = newScore
}

func TestChangeStudent(t *testing.T) {
    s1 := Student{
        Name:  "whw",
        Score: 99,
    }
    changeStudent(&s1, "naruto", 100)
    fmt.Println("s1: ", s1) // s1:  {naruto 100}
}

~~~

posted on 2022-04-11 15:36  江湖乄夜雨  阅读(398)  评论(0编辑  收藏  举报