企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
> # []struct{} 如何遍历 ~~~ package main import "fmt" type Item struct { Id int Val string } func main() { item := make([]Item, 0, 1024) for i := 0; i < 1000; i++ { item = append(item, Item{ Id: i, Val: fmt.Sprintf("%d", i), }) } //遍历1 for k, v := range item { _ = v //值类型会拷贝 _ = k } //遍历1 for k, _ := range item { _ = item[k] //不会拷贝副本 _ = k } item2 := make([]*Item, 0, 1024) for i := 0; i < 1000; i++ { item2 = append(item2, &Item{ Id: i, Val: fmt.Sprintf("%d", i), }) } for k, v := range item2 { _ = v //指针不会 拷贝副本 _ = k } } ~~~