use of variable in for loop not recognized in Golang

无人久伴 提交于 2019-12-02 01:23:44

The Golang compiler doesn't seem to recognise that the i variable is given back to the function in the next loop. Inside this function, the I variable changes value.

No, i does not change value; := declares a new i. (Go allows you to do this because data is also new.) To assign to it instead, you’ll need to declare data separately:

var data RandomDataType
data, i = GiveRandomData(i)

Or give the new i a temporary name:

data, next := GiveRandomData(i)
i = next
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!