Go 語言中數組可以存儲同一類型的數據,但在結構體中我們可以為不同項定義不同的數據類型。
結構體是由一系列具有相同類型或不同類型的數據構成的數據集合。
## 一、聲明方式
~~~
variable_name := structure_variable_type {value1, value2...valuen}
或
variable_name := structure_variable_type { key1: value1, key2: value2..., keyn: valuen}
~~~
**示例:**
~~~
package main
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
comment, remarks string // 多個聲明
}
func main() {
// 創建一個新的結構體
fmt.Println(Books{"Go 語言", "www.runoob.com", "Go 語言教程", 6495407, "不錯", "加油!"})
// 也可以使用 key => value 格式
fmt.Println(Books{title: "Go 語言", author: "www.runoob.com", subject: "Go 語言教程", book_id: 6495407})
// 忽略的字段為 0 或 空
fmt.Println(Books{title: "Go 語言", author: "www.runoob.com"})
}
~~~
結果:
~~~
{Go 語言 www.runoob.com Go 語言教程 6495407 不錯 加油!}
{Go 語言 www.runoob.com Go 語言教程 6495407 }
{Go 語言 www.runoob.com 0 }
~~~
## 二、結構體作為函數參數
~~~
package main
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
var Book1 Books /* 聲明 Book1 為 Books 類型 */
var Book2 Books /* 聲明 Book2 為 Books 類型 */
/* book 1 描述 */
Book1.title = "Go 語言"
Book1.author = "www.runoob.com"
Book1.subject = "Go 語言教程"
Book1.book_id = 6495407
/* book 2 描述 */
Book2.title = "Python 教程"
Book2.author = "www.runoob.com"
Book2.subject = "Python 語言教程"
Book2.book_id = 6495700
/* 打印 Book1 信息 */
printBook(Book1)
/* 打印 Book2 信息 */
printBook(Book2)
}
func printBook( book Books ) {
fmt.Printf( "Book title : %s\n", book.title);
fmt.Printf( "Book author : %s\n", book.author);
fmt.Printf( "Book subject : %s\n", book.subject);
fmt.Printf( "Book book_id : %d\n", book.book_id);
}
~~~
結果:
~~~
Book title : Go 語言
Book author : www.runoob.com
Book subject : Go 語言教程
Book book_id : 6495407
Book title : Python 教程
Book author : www.runoob.com
Book subject : Python 語言教程
Book book_id : 6495700
~~~
## 三、結構體指針
~~~
package main
import "fmt"
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
var Book1 Books /* Declare Book1 of type Book */
var Book2 Books /* Declare Book2 of type Book */
/* book 1 描述 */
Book1.title = "Go 語言"
Book1.author = "www.runoob.com"
Book1.subject = "Go 語言教程"
Book1.book_id = 6495407
/* book 2 描述 */
Book2.title = "Python 教程"
Book2.author = "www.runoob.com"
Book2.subject = "Python 語言教程"
Book2.book_id = 6495700
/* 打印 Book1 信息 */
printBook(&Book1)
/* 打印 Book2 信息 */
printBook(&Book2)
}
func printBook( book *Books ) {
fmt.Printf( "Book title : %s\n", book.title);
fmt.Printf( "Book author : %s\n", book.author);
fmt.Printf( "Book subject : %s\n", book.subject);
fmt.Printf( "Book book_id : %d\n", book.book_id);
}
~~~
結果:
~~~
Book title : Go 語言
Book author : www.runoob.com
Book subject : Go 語言教程
Book book_id : 6495407
Book title : Python 教程
Book author : www.runoob.com
Book subject : Python 語言教程
Book book_id : 6495700
~~~
## 四、Struct Inherit 結構體繼承
```
package main
import "fmt"
type A struct {
title string
author string
subject string
book_id int
comment, remarks string // 多個聲明
}
type B struct {
A
time int
}
func main() {
var b B
b.time = 10
b.A.title = "hello A title."
fmt.Println(b.time)
fmt.Println(b.A.title)
b.A = A{title: "hi title.", author: "zhangsan", subject: "hi subject."}
fmt.Println(b.A)
}
```
結果:
```
10
hello A title.
{hi title. zhangsan hi subject. 0 }
```
## 五、Struct Tag 結構體標簽
**示例1:**
~~~
package main
import (
"encoding/json"
"fmt"
"gopkg.in/mgo.v2/bson" // 下載:go get gopkg.in/mgo.v2/bson (http://labix.org/gobson)
)
type User struct {
Id int `json:"id" bson:"id"`
Name string `json:"name" bson:"name"`
}
func main() {
u := &User{Id: 1, Name: "tony"}
j, _ := json.Marshal(u)
b, _ := bson.Marshal(u)
fmt.Println(string(j))
fmt.Printf("%q", b)
}
~~~
結果:

**示例2:** 獲取標簽tag
~~~
package main
import (
"fmt"
"reflect"
)
type User struct {
Id int `json:"id" bson:"id"`
Name string `json:"name" bson:"name"`
}
func main() {
u := &User{Id: 1, Name: "tony"}
// 獲取標簽tag
t := reflect.TypeOf(u)
fmt.Println(t.Elem().NumField()) // 2個字段
field := t.Elem().Field(0)
fmt.Println(field.Tag.Get("json")) // id
fmt.Println(field.Tag.Get("bson")) // id
}
~~~