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)
}
~~~
结果:
![](https://box.kancloud.cn/a818a438aa8282019d6f0a951b22938c_1154x70.png)
**示例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
}
~~~