翻译自 https://gobyexample.com/
Go by Example
Go is an open source programming language designed for building simple, fast, and reliable software.
Go by Example is a hands-on introduction to Go using annotated example programs. Check out the first example or browse the full list below.
Go by Example: Structs
golang中定义结构体为一系列元素的集合,这对于将数据分组记录很有用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| package main
import "fmt"
type person struct { name string age int }
func main() { fmt.Println(person{"Bob", 20})
fmt.Println(person{name: "Alice", age: 30}) fmt.Println(person{name: "Fred"})
fmt.Println(&person{name: "Ann", age: 40})
s := person{name: "Sean", age: 50} fmt.Println(s.name) sp := &s fmt.Println(sp.age)
sp.age = 51 fmt.Println(sp.age) }
|
1 2 3 4 5 6 7 8
| $ go run structs.go {Bob 20} {Alice 30} {Fred 0} &{Ann 40} Sean 50 51
|
原文链接:Go by Example: Structs