
- Go 教程
- Go - 首頁
- Go - 概述
- Go - 環境設定
- Go - 程式結構
- Go - 基本語法
- Go - 資料型別
- Go - 變數
- Go - 常量
- Go - 運算子
- Go - 決策制定
- Go - 迴圈
- Go - 函式
- Go - 作用域規則
- Go - 字串
- Go - 陣列
- Go - 指標
- Go - 結構體
- Go - 切片
- Go - 範圍
- Go - 對映
- Go - 遞迴
- Go - 型別轉換
- Go - 介面
- Go - 錯誤處理
- Go 有用資源
- Go - 問題與解答
- Go - 快速指南
- Go - 有用資源
- Go - 討論
Go - 結構體
Go 陣列允許您定義可以儲存幾種相同型別的資料項的變數。結構體是 Go 程式設計中另一種使用者定義的資料型別,它允許您組合不同型別的資料項。
結構體用於表示記錄。假設您想跟蹤圖書館中的書籍。您可能希望跟蹤每本書的以下屬性:
- 標題
- 作者
- 主題
- 圖書 ID
在這種情況下,結構體非常有用。
定義結構體
要定義結構體,必須使用type 和struct 語句。struct 語句定義了一種新的資料型別,其中包含程式的多個成員。type 語句將名稱與型別繫結,在本例中型別為 struct。struct 語句的格式如下:
type struct_variable_type struct { member definition; member definition; ... member definition; }
一旦定義了結構體型別,就可以使用以下語法宣告該型別的變數。
variable_name := structure_variable_type {value1, value2...valuen}
訪問結構體成員
要訪問結構體的任何成員,我們使用成員訪問運算子 (.)。成員訪問運算子在結構體變數名和我們希望訪問的結構體成員之間編碼為一個句點。您將使用struct 關鍵字定義結構體型別的變數。以下示例說明如何使用結構體:
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 specification */ Book1.title = "Go Programming" Book1.author = "Mahesh Kumar" Book1.subject = "Go Programming Tutorial" Book1.book_id = 6495407 /* book 2 specification */ Book2.title = "Telecom Billing" Book2.author = "Zara Ali" Book2.subject = "Telecom Billing Tutorial" Book2.book_id = 6495700 /* print Book1 info */ 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) /* print Book2 info */ 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) }
當編譯並執行以上程式碼時,會產生以下結果:
Book 1 title : Go Programming Book 1 author : Mahesh Kumar Book 1 subject : Go Programming Tutorial Book 1 book_id : 6495407 Book 2 title : Telecom Billing Book 2 author : Zara Ali Book 2 subject : Telecom Billing Tutorial Book 2 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 specification */ Book1.title = "Go Programming" Book1.author = "Mahesh Kumar" Book1.subject = "Go Programming Tutorial" Book1.book_id = 6495407 /* book 2 specification */ Book2.title = "Telecom Billing" Book2.author = "Zara Ali" Book2.subject = "Telecom Billing Tutorial" Book2.book_id = 6495700 /* print Book1 info */ printBook(Book1) /* print Book2 info */ 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 Programming Book author : Mahesh Kumar Book subject : Go Programming Tutorial Book book_id : 6495407 Book title : Telecom Billing Book author : Zara Ali Book subject : Telecom Billing Tutorial Book book_id : 6495700
指向結構體的指標
您可以像定義指向任何其他變數的指標一樣,定義指向結構體的指標,如下所示:
var struct_pointer *Books
現在,您可以將結構體變數的地址儲存在上面定義的指標變數中。要查詢結構體變數的地址,請在結構體名稱前放置 & 運算子,如下所示:
struct_pointer = &Book1;
要使用指向該結構體的指標訪問結構體的成員,必須使用 "." 運算子,如下所示:
struct_pointer.title;
讓我們使用結構體指標重寫上述示例:
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 specification */ Book1.title = "Go Programming" Book1.author = "Mahesh Kumar" Book1.subject = "Go Programming Tutorial" Book1.book_id = 6495407 /* book 2 specification */ Book2.title = "Telecom Billing" Book2.author = "Zara Ali" Book2.subject = "Telecom Billing Tutorial" Book2.book_id = 6495700 /* print Book1 info */ printBook(&Book1) /* print Book2 info */ 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 Programming Book author : Mahesh Kumar Book subject : Go Programming Tutorial Book book_id : 6495407 Book title : Telecom Billing Book author : Zara Ali Book subject : Telecom Billing Tutorial Book book_id : 6495700
廣告