在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ GO/ Go語言結(jié)構(gòu)體
Go panic錯誤處理實(shí)例
Go命令行參數(shù)實(shí)例
Go可變參數(shù)的函數(shù)實(shí)例
Go通道同步實(shí)例
Go非阻塞通道操作實(shí)例
Go指針實(shí)例
Go數(shù)字解析實(shí)例
Go語言指針
Go超時(timeouts)實(shí)例
Go速率限制實(shí)例
Go信號實(shí)例
Go Base64編碼實(shí)例
Go計(jì)時器實(shí)例
Go命令行標(biāo)志實(shí)例
Go原子計(jì)數(shù)器實(shí)例
Go語言切片
Go隨機(jī)數(shù)實(shí)例
Go語言類型轉(zhuǎn)換
Go排序?qū)嵗?/span>
Go時間格式化/解析實(shí)例
Go URL解析實(shí)例
Go字符串函數(shù)實(shí)例
Go語言常量
Go for循環(huán)語句實(shí)例
Go函數(shù)多個返回值實(shí)例
Go切片實(shí)例
Go行過濾器實(shí)例
Go語言接口
Go語言數(shù)組
Go語言變量
Go字符串格式化實(shí)例
Go斷續(xù)器實(shí)例
Go if/else語句實(shí)例
Go通道緩沖實(shí)例
Go錯誤實(shí)例
Go語言映射
Go執(zhí)行過程實(shí)例
Go函數(shù)實(shí)例
Go有狀態(tài)的goroutines實(shí)例
Go按自定義函數(shù)排序?qū)嵗?/span>
Go語言作用域規(guī)則
Go時代(Epoch)實(shí)例
Go變量實(shí)例
Go互斥體實(shí)例
Go語言范圍(range)
Go程序?qū)嵗?/span>
Go語言入門
Go通道路線實(shí)例
Go閉包(匿名函數(shù))實(shí)例
Go Select實(shí)例
Go通道范圍實(shí)例
Go集合函數(shù)實(shí)例
Hello World程序?qū)嵗?/span>
Go環(huán)境變量實(shí)例
Go語言運(yùn)算符
Go讀取文件實(shí)例
Go延遲(defer)實(shí)例
Go SHA1哈希實(shí)例
Go語言條件和決策
Go語言錯誤處理
Go通道實(shí)例
Go指針實(shí)例
Go時間日期實(shí)例
Go語言字符串
Go語言循環(huán)
Go語言基礎(chǔ)語法
Go語言開發(fā)環(huán)境安裝配置
Go常量實(shí)例
Go語言結(jié)構(gòu)體
Go寫文件實(shí)例
Go正則表達(dá)式實(shí)例
Go JSON實(shí)例
Go語言教程
Go關(guān)閉通道實(shí)例
Go接口實(shí)例
Go語言遞歸
Go switch語句實(shí)例
Go函數(shù)遞歸實(shí)例
Go退出程序?qū)嵗?/span>
Go語言程序結(jié)構(gòu)
Go范圍實(shí)例
Go語言函數(shù)
Go工作池實(shí)例
Go語言數(shù)據(jù)類型

Go語言結(jié)構(gòu)體

Go數(shù)組允許定義一種可以保存多個相同類型的數(shù)據(jù)項(xiàng)的變量類型,但結(jié)構(gòu)體是Go編程中可用的另一個用戶定義的數(shù)據(jù)類型,它允許組合不同類型的數(shù)據(jù)項(xiàng)。

結(jié)構(gòu)體用于表示記錄,假設(shè)想在圖書館中跟蹤圖書??赡芟M櫭勘緢D書的以下屬性:

  • 標(biāo)題(Title)
  • 作者(Author)
  • 科目(Subject)
  • 圖書編號(Book ID)

定義結(jié)構(gòu)體

要定義結(jié)構(gòu),必須使用typestruct語句。struct語句定義了一個新的數(shù)據(jù)類型,在程序中有多個成員。type語句在例子中綁定一個類型為struct的名字。 struct語句的格式如下:

type struct_variable_type struct {
   member definition;
   member definition;
   ...
   member definition;
}

當(dāng)定義了結(jié)構(gòu)體類型,它可以用于使用以下語法聲明該類型的變量。

variable_name := structure_variable_type {value1, value2...valuen}

訪問結(jié)構(gòu)體成員

要訪問結(jié)構(gòu)的任何成員,可使用成員訪問運(yùn)算符(.)。成員訪問運(yùn)算符被編碼為結(jié)構(gòu)變量名稱和希望訪問的結(jié)構(gòu)成員的名稱。使用struct關(guān)鍵字定義結(jié)構(gòu)類型的變量。以下是解釋結(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 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)
}

當(dāng)上述代碼編譯和執(zhí)行時,它產(chǎn)生以下結(jié)果:

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

結(jié)構(gòu)體作為函數(shù)參數(shù)

可以傳遞一個結(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        /* 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);
}

當(dāng)上述代碼編譯和執(zhí)行時,它產(chǎn)生以下結(jié)果:

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

指針指向結(jié)構(gòu)

可以以非常類似于定義指向其他變量的指針的方式來定義結(jié)構(gòu)的指針,如下所示:

var struct_pointer *Books

現(xiàn)在,可以在上面定義的指針變量中存儲結(jié)構(gòu)體變量的地址。要查找結(jié)構(gòu)體變量的地址,將&操作符放在結(jié)構(gòu)體名稱之前,如下:

struct_pointer = &Book1;

要使用指向該結(jié)構(gòu)體的指針來訪問結(jié)構(gòu)的成員,必須使用“.”。 運(yùn)算符如下:

struct_pointer.title;

現(xiàn)在,重寫上面的例子使用結(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 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);
}

當(dāng)上述代碼編譯和執(zhí)行時,它產(chǎn)生以下結(jié)果:

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