位置:首頁 > 高級語言 > Go語言教學 > Go語言結構

Go語言結構

Go數組允許定義類型的變量,可容納同類的多個數據項,但結構在Go編程,它允許結合不同種類的數據項,可使用其他用戶定義的數據類型。

結構是用來代表一個記錄,假設要跟蹤圖書館的圖書。可能要跟蹤有關每本書以下屬性:

  • Title(書名)

  • Author(作者)

  • Subject(科目)

  • Book ID(編號)

定義結構

定義一個結構,必須使用type和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