本人刚学习go,再体验range 遍历通道的遍历之时,又产生了一些误解,对于for data:=range 通道中data我之前一直以为是每次for循环都会通过:=新定义一个data变量,但实际上并非如此,只是第一次会定义变量data,在后面的每次循环都是重复使用。这在官方文档里面,可以看到:The iteration variables may be declared by the "range" clause using a form of short variable declaration (:=). In this case their types are set to the types of the respective iteration values and their scope is the block of the "for" statement; they are re-used in each iteration.
有疑问加站长微信联系(非本文作者)