Go陣列允許定義一種可以儲存多個相同型別的資料項的變數型別,但結構體是Go程式設計中可用的另一個使用者定義的資料型別,它允許組合不同型別的資料項。
結構體用於表示記錄,假設想在圖書館中跟蹤圖書。可能希望跟蹤每本圖書的以下屬性:
要定義結構,必須使用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
結構體就像是物件導向中的類一樣,結構體中的成員就像是類中的成員一樣。 提交時間:2019-09-02