One - One Code All

Blog Content

go语言结构体列表追加,中如何实现动态添加自定义的结构体到一个集合?

Go   2018-09-08 14:53:30
type ImageData struct {
    src    string
    tp     string
    title  string
    width  int
    height int
}

type ImageDatas []ImageData

// 现在我这样得到一个ImageData:
imageData := ImageData{src, tp, title, wd, hg}
// 但是怎么把imageData加入到ImageDatas中呢?最后怎么遍历出所有数据呢?

imagedatas = append(imagedatas, imagedata)

参考示例:

https://gobyexample.com/slices

s := make([]string, 3)
fmt.Println("emp:", s)
s[0] = "a"
s[1] = "b"
s[2] = "c"
fmt.Println("set:", s)
fmt.Println("get:", s[2])
fmt.Println("len:", len(s))

s = append(s, "d")
s = append(s, "e", "f")
fmt.Println("apd:", s)



上一篇:github.com/gogo/protobuf/gogoproto/gogo.proto: File not found.
下一篇:go语言时间和时间戳转换

The minute you think of giving up, think of the reason why you held on so long.