Go 語言結(jié)構(gòu)體
Go 語言中數(shù)組可以存儲同一類型的數(shù)據(jù),但在結(jié)構(gòu)體中我們可以為不同項定義不同的數(shù)據(jù)類型。
結(jié)構(gòu)體是由一系列具有相同類型或不同類型的數(shù)據(jù)構(gòu)成的數(shù)據(jù)集合。
結(jié)構(gòu)體表示一項記錄,比如保存圖書館的書籍記錄,每本書有以下屬性:
Title :標題
Author : 作者
Subject:學科
ID:書籍ID
定義結(jié)構(gòu)體
結(jié)構(gòu)體定義需要使用 type 和 struct 語句。struct 語句定義一個新的數(shù)據(jù)類型,結(jié)構(gòu)體有中一個或多個成員。type 語句設定了結(jié)構(gòu)體的名稱。結(jié)構(gòu)體的格式如下:
type struct_variable_type struct { member definition; member definition; ... member definition; }
一旦定義了結(jié)構(gòu)體類型,它就能用于變量的聲明,語法格式如下:
variable_name := structure_variable_type {value1, value2...valuen}
訪問結(jié)構(gòu)體成員
如果要訪問結(jié)構(gòu)體成員,需要使用點號 (.) 操作符,格式為:"結(jié)構(gòu)體.成員名"。
結(jié)構(gòu)體類型變量使用struct關(guān)鍵字定義,實例如下:
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 = "miracleart.cn" Book1.subject = "Go 語言教程" Book1.book_id = 6495407 /* book 2 描述 */ Book2.title = "Python 教程" Book2.author = "miracleart.cn" Book2.subject = "Python 語言教程" Book2.book_id = 6495700 /* 打印 Book1 信息 */ fmt.Printf( "Book 1 title : %s\n", Book1.title) fmt.Printf( "Book 1 author : %s\n", Book1.author) fmt.Printf( "Book 1 subject : %s\n", Book1.subject) fmt.Printf( "Book 1 book_id : %d\n", Book1.book_id) /* 打印 Book2 信息 */ fmt.Printf( "Book 2 title : %s\n", Book2.title) fmt.Printf( "Book 2 author : %s\n", Book2.author) fmt.Printf( "Book 2 subject : %s\n", Book2.subject) fmt.Printf( "Book 2 book_id : %d\n", Book2.book_id) }
以上實例執(zhí)行運行結(jié)果為:
Book 1 title : Go 語言 Book 1 author : miracleart.cn Book 1 subject : Go 語言教程 Book 1 book_id : 6495407 Book 2 title : Python 教程 Book 2 author : miracleart.cn Book 2 subject : Python 語言教程 Book 2 book_id : 6495700
結(jié)構(gòu)體作為函數(shù)參數(shù)
你可以向其他數(shù)據(jù)類型一樣將結(jié)構(gòu)體類型作為參數(shù)傳遞給函數(shù)。并以以上實例的方式訪問結(jié)構(gòu)體變量:
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 = "miracleart.cn" Book1.subject = "Go 語言教程" Book1.book_id = 6495407 /* book 2 描述 */ Book2.title = "Python 教程" Book2.author = "miracleart.cn" 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); }
以上實例執(zhí)行運行結(jié)果為:
Book title : Go 語言 Book author : miracleart.cn Book subject : Go 語言教程 Book book_id : 6495407 Book title : Python 教程 Book author : miracleart.cn Book subject : Python 語言教程 Book book_id : 6495700
結(jié)構(gòu)體指針
你可以定義指向結(jié)構(gòu)體的指針類似于其他指針變量,格式如下:
var struct_pointer *Books
以上定義的指針變量可以存儲結(jié)構(gòu)體變量的地址。查看結(jié)構(gòu)體變量地址,可以將 & 符號放置于結(jié)構(gòu)體變量前:
struct_pointer = &Book1;
使用結(jié)構(gòu)體指針訪問結(jié)構(gòu)體成員,使用 "." 操作符:
struct_pointer.title;
接下來讓我們使用結(jié)構(gòu)體指針重寫以上實例,代碼如下:
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 = "miracleart.cn" Book1.subject = "Go 語言教程" Book1.book_id = 6495407 /* book 2 描述 */ Book2.title = "Python 教程" Book2.author = "miracleart.cn" 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); }
以上實例執(zhí)行運行結(jié)果為:
Book title : Go 語言 Book author : miracleart.cn Book subject : Go 語言教程 Book book_id : 6495407 Book title : Python 教程 Book author : miracleart.cn Book subject : Python 語言教程 Book book_id : 6495700