Go语言教程之边写边学:golang中创建结构体切片

示例代码:

package main

import (
	"fmt"
)

type Widget struct {
	id    int
	attrs []string
}

func main() {

	widgets := []Widget{
		Widget{
			id:    10,
			attrs: []string{"blah", "foo"},
		},
		Widget{
			id:    11,
			attrs: []string{"foo", "bar"},
		},
		Widget{
			id:    12,
			attrs: []string{"xyz"},
		},
	}

	for _, j := range widgets {
		fmt.Printf("%d ", j.id)
		for _, y := range j.attrs {
			fmt.Printf(" %s ", y)
		}
		fmt.Println()
	}
}

输出:

10  blah  foo 
11  foo  bar
12  xyz
  • 当前日期:
  • 北京时间:
  • 时间戳:
  • 今年的第:19周
  • 我的 IP:3.142.124.139
农历
五行
冲煞
彭祖
方位
吉神
凶神
极简任务管理 help
+ 0 0 0
Task Idea Collect