2021
05-25
05-25
golang中for range的取地址操作陷阱介绍
Tips:forrange创建了每个元素的副本,而不是直接返回每个元素的引用例子1:packagemainimport"fmt"funcmain(){slice:=[]int{0,1,2,3}myMap:=make(map[int]*int)forindex,value:=rangeslice{myMap[index]=&value}fmt.Println("=====newmap=====")prtMap(myMap)}funcprtMap(myMapmap[int]*int){forkey,value:=rangemyMap{fmt.Printf("map[%v]=%v\n",key,*value)}}输出:dotzdeMacBook...
继续阅读 >