我来自C++背景,我习惯于使用std::vector类来处理这样的事情.

type a struct {
    b int
    c string
}

执行此操作的标准方法是什么?

A snippet would be very useful

推荐答案

使用append()内置

示例:

type mytype struct {
  a, b int
}

func main() {
  a := []mytype{mytype{1, 2}, mytype{3, 4}}
  a = append(a, mytype{5, 6})
}

有关追加的更多信息,请参阅spec.

Go相关问答推荐

Go程序在并发Forking 循环中停留在syscall.Wait4

无法获取RPC描述符

如何解析Go-Gin多部分请求中的 struct 切片

go测试10m后如何避免超时

Golang text/template中的startswith函数 - 入门教程

Go test "-run -" 标志执行测试更快

MQTT 客户端没有收到另一个客户端发送的消息

使用 Go 解组 SOAP 消息

go-libp2p - 从流中接收字节

在 Gorm 的 AfterFind() 钩子中获取智能 Select struct 的值

为什么此代码在运行命令 error="exec: not started" 时出现错误?

为什么 0 big.Int 的 .Bytes() 值是空切片?

使用正则表达式拆分具有相同标题的数据块

go 堆栈跟踪:在某些函数调用参数或返回值之后的问题(?)标记是什么意思?

Go:用于 XML 解码的嵌套 struct 中的提升字段

Golang 泛型

Golang 有类似 C++ 的 decltype 的东西吗?

行之间的模板交替设计

为什么 template.ParseFiles() 没有检测到这个错误?

如何断言类型是指向golang中接口的指针