我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

声明struct文字数组

How do I declare an array of struct literal?

Go:

type Ping struct {
    Content []aContent
}

type aContent struct {
    Type        string
    Id          string
    Created_at  int64
}

func main() {
    f := Ping{Content: []aContent{Type: "Hello", Id: "asdf"}}
    fmt.Println(f)
}

The code can be found here: http://play.golang.org/p/-SyRw6dDUm

You just need another pair of braces.

[]aContent{{Type: "Hello", Id: "asdf"}, {Type: "World", Id: "ghij"}}}
           ^                                                      ^
         here                                                    and here

That's one pair for the array, one for each struct in the array..

近期文章

  • Golang:使用log.Println登录文件是否照顾并发访问
  • 如何在字符串上使用gzip?
  • 如何通过Golang程序设置ulimit -n?
  • Go项目中需要main.go吗?
  • 在Golang中将[]接口转换为[]字符串
  • 将int设置为0的Unmarshal / Marshal json似乎不起作用
  • 如何确定goroutine是否已完成而没有阻塞?
  • GOlang:动态变量
  • Go中命名返回变量的预期用途是什么?
  • Mgo即使不为空也忽略字段
  • 缓冲/非缓冲通道
  • 如何用Go语言编译程序?
  • 去闭包变量范围
  • golang地图打印混乱
  • 如何在Jinja2中渲染时转义双引号?
  • Atom编辑器Golang-转到声明不起作用
  • 如何避免重新实现sort.interface类似golang结构
  • Golang AES ECB加密
  • golang:在地图中快速访问地图数据
  • 您能检测给定数量的goroutine将创建多少个线程吗?

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华