?
Ce document utilise Manuel du site Web PHP chinois Libérer
Go 語言中數(shù)組可以存儲同一類型的數(shù)據(jù),但在結(jié)構(gòu)體中我們可以為不同項定義不同的數(shù)據(jù)類型。
結(jié)構(gòu)體是由一系列具有相同類型或不同類型的數(shù)據(jù)構(gòu)成的數(shù)據(jù)集合。
結(jié)構(gòu)體表示一項記錄,比如保存圖書館的書籍記錄,每本書有以下屬性:
結(jié)構(gòu)體定義需要使用 type 和 struct 語句。struct 語句定義一個新的數(shù)據(jù)類型,結(jié)構(gòu)體有中一個或多個成員。type 語句設(shè)定了結(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)體類型變量使用struct關(guān)鍵字定義,實例如下:
package main import "fmt" type Books struct { title string author string subject string book_id int } func main() { var Book1 Books var Book2 Books Book1.title = "Go 語言" Book1.author = "www.shouce.ren" Book1.subject = "Go 語言教程" Book1.book_id = 6495407 Book2.title = "Python 教程" Book2.author = "www.shouce.ren" Book2.subject = "Python 語言教程" Book2.book_id = 6495700 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) 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í)行運(yùn)行結(jié)果為:
Book 1 title : Go 語言 Book 1 author : www.shouce.ren Book 1 subject : Go 語言教程 Book 1 book_id : 6495407 Book 2 title : Python 教程 Book 2 author : www.shouce.ren Book 2 subject : Python 語言教程 Book 2 book_id : 6495700
你可以向其他數(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 var Book2 Books Book1.title = "Go 語言" Book1.author = "www.shouce.ren" Book1.subject = "Go 語言教程" Book1.book_id = 6495407 Book2.title = "Python 教程" Book2.author = "www.shouce.ren" Book2.subject = "Python 語言教程" Book2.book_id = 6495700 printBook(Book1) 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í)行運(yùn)行結(jié)果為:
Book title : Go 語言 Book author : www.shouce.ren Book subject : Go 語言教程 Book book_id : 6495407 Book title : Python 教程 Book author : www.shouce.ren Book subject : Python 語言教程 Book book_id : 6495700
你可以定義指向結(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 var Book2 Books Book1.title = "Go 語言" Book1.author = "www.shouce.ren" Book1.subject = "Go 語言教程" Book1.book_id = 6495407 Book2.title = "Python 教程" Book2.author = "www.shouce.ren" Book2.subject = "Python 語言教程" Book2.book_id = 6495700 printBook(&Book1) 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í)行運(yùn)行結(jié)果為:
Book title : Go 語言 Book author : www.shouce.ren Book subject : Go 語言教程 Book book_id : 6495407 Book title : Python 教程 Book author : www.shouce.ren Book subject : Python 語言教程 Book book_id : 6495700
關(guān)于我們 聯(lián)系我們 留言板
手冊網(wǎng)