首页 > go语言中如何实现动态添加自定义的结构体到一个集合?

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

刚开始学习go语言,我自定义了一个结构体:

type ImageData struct {
    src    string
    tp     string
    title  string
    width  int
    height int
}

然后我获取了很多条这样的数据,想将每一条数据加到一个大的集合中形成类似这样的:

ImageDatas{
    {"1111", "111111", "111111",100, 100},
    {"22", "222", "222",100, 100},
    {"33", "333333", "333333",100, 100},
}

我定义了一个ImageDatas

type ImageDatas []ImageData

现在我这样得到一个ImageData

imageData := ImageData{src, tp, title, wd, hg}

但是怎么把imageData加入到ImageDatas中呢?最后怎么遍历出所有数据呢?


https://gobyexample.com/slices


imagedatas = append(imagedatas, imagedata)

【热门文章】
【热门文章】