package main
import (
"fmt"
"sync"
)
var wg sync.WaitGroup
func changePoint(count *int) {
defer wg.Done()
nowVal := 7
//這種情況在main函數(shù)中count指向的值不會更新
//count = &nowVal
//這種情況在main函數(shù)中count指向的值會更新
*count = nowVal
}
func ExampleGoroutine(orgId string) {
initVal := 5
count := &initVal
wg.Add(1)
go changePoint(count)
wg.Wait()
fmt.Println(*count)
}
func main() {
ExampleGoroutine("test")
}
碰巧遇到這個問題娩鹉。changePoint 函數(shù)傳入的指針是值拷貝氓仲,所以入?yún)ount的作用域只在changePoint函數(shù)內颜凯。
需要修改count指向對象的值的話罢防,需要直接對其指向的地址進行賦值蹲缠,而不是把新值的地址賦給它