Skip to content

dockerq/go-pointer-trick

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Golang for...range

关键代码:

for k, r := range *rr {
    fmt.Printf("%dth r, id: %d, cpu: %f, mem: %f\n", k, r.ID, r.CPU, r.MEM)
    rs = append(rs, &r)
}

关键在for...range,for循环中的k,r是临时变量它们在确定之后地址不会改变,但是值可以改变。rs = append(rs, &r)这里&r取r的地址一直是同一个值,循环结束后r的值是最后一次循环的值。

解决方法是是使用索引

for k, _ := range *rr {
    rs = append(rs, &(*r)[k])
}

参考:聊聊Go中的Range关键字

About

golang for...range trick

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages