中文字幕一区二区人妻电影,亚洲av无码一区二区乱子伦as ,亚洲精品无码永久在线观看,亚洲成aⅴ人片久青草影院按摩,亚洲黑人巨大videos

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

Go 語言中數(shù)組可以存儲同一類型的數(shù)據(jù),但在結(jié)構(gòu)體中我們可以為不同項(xiàng)定義不同的數(shù)據(jù)類型。

結(jié)構(gòu)體是由一系列具有相同類型或不同類型的數(shù)據(jù)構(gòu)成的數(shù)據(jù)集合。

結(jié)構(gòu)體表示一項(xiàng)記錄,比如保存圖書館的書籍記錄,每本書有以下屬性:

  • Title :標(biāo)題
  • Author : 作者
  • Subject:學(xué)科
  • ID:書籍ID

定義結(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}
或
variable_name := structure_variable_type { key1: value1, key2: value2..., keyn: valuen}

實(shí)例如下:

實(shí)例

package main

import "fmt"

type Books struct {
? ?title string
? ?author string
? ?subject string
? ?book_id int
}


func main() {

? ? // 創(chuàng)建一個新的結(jié)構(gòu)體
? ? fmt.Println(Books{"Go 語言", "", "Go 語言教程", 6495407})

? ? // 也可以使用 key => value 格式
? ? fmt.Println(Books{title: "Go 語言", author: "", subject: "Go 語言教程", book_id: 6495407})

? ? // 忽略的字段為 0 或 空
? ?fmt.Println(Books{title: "Go 語言", author: ""})
}

輸出結(jié)果為:

{Go 語言  Go 語言教程 6495407}
{Go 語言  Go 語言教程 6495407}
{Go 語言   0}

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

如果要訪問結(jié)構(gòu)體成員,需要使用點(diǎn)號 . 操作符,格式為:

結(jié)構(gòu)體.成員名"

結(jié)構(gòu)體類型變量使用 struct 關(guān)鍵字定義,實(shí)例如下:

實(shí)例

package main

import "fmt"

type Books struct {
? ?title string
? ?author string
? ?subject string
? ?book_id int
}

func main() {
? ?var Book1 Books ? ? ? ?/* 聲明 Book1 為 Books 類型 */
? ?var Book2 Books ? ? ? ?/* 聲明 Book2 為 Books 類型 */

? ?/* book 1 描述 */
? ?Book1.title = "Go 語言"
? ?Book1.author = ""
? ?Book1.subject = "Go 語言教程"
? ?Book1.book_id = 6495407

? ?/* book 2 描述 */
? ?Book2.title = "Python 教程"
? ?Book2.author = ""
? ?Book2.subject = "Python 語言教程"
? ?Book2.book_id = 6495700

? ?/* 打印 Book1 信息 */
? ?fmt.Printf( "Book 1 title : %sn", Book1.title)
? ?fmt.Printf( "Book 1 author : %sn", Book1.author)
? ?fmt.Printf( "Book 1 subject : %sn", Book1.subject)
? ?fmt.Printf( "Book 1 book_id : %dn", Book1.book_id)

? ?/* 打印 Book2 信息 */
? ?fmt.Printf( "Book 2 title : %sn", Book2.title)
? ?fmt.Printf( "Book 2 author : %sn", Book2.author)
? ?fmt.Printf( "Book 2 subject : %sn", Book2.subject)
? ?fmt.Printf( "Book 2 book_id : %dn", Book2.book_id)
}

以上實(shí)例執(zhí)行運(yùn)行結(jié)果為:

Book 1 title : Go 語言
Book 1 author : 
Book 1 subject : Go 語言教程
Book 1 book_id : 6495407
Book 2 title : Python 教程
Book 2 author : 
Book 2 subject : Python 語言教程
Book 2 book_id : 6495700

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

你可以像其他數(shù)據(jù)類型一樣將結(jié)構(gòu)體類型作為參數(shù)傳遞給函數(shù)。并以以上實(shí)例的方式訪問結(jié)構(gòu)體變量:

實(shí)例

package main

import "fmt"

type Books struct {
? ?title string
? ?author string
? ?subject string
? ?book_id int
}

func main() {
? ?var Book1 Books ? ? ? ?/* 聲明 Book1 為 Books 類型 */
? ?var Book2 Books ? ? ? ?/* 聲明 Book2 為 Books 類型 */

? ?/* book 1 描述 */
? ?Book1.title = "Go 語言"
? ?Book1.author = ""
? ?Book1.subject = "Go 語言教程"
? ?Book1.book_id = 6495407

? ?/* book 2 描述 */
? ?Book2.title = "Python 教程"
? ?Book2.author = ""
? ?Book2.subject = "Python 語言教程"
? ?Book2.book_id = 6495700

? ?/* 打印 Book1 信息 */
? ?printBook(Book1)

? ?/* 打印 Book2 信息 */
? ?printBook(Book2)
}

func printBook( book Books ) {
? ?fmt.Printf( "Book title : %sn", book.title)
? ?fmt.Printf( "Book author : %sn", book.author)
? ?fmt.Printf( "Book subject : %sn", book.subject)
? ?fmt.Printf( "Book book_id : %dn", book.book_id)
}

以上實(shí)例執(zhí)行運(yùn)行結(jié)果為:

Book title : Go 語言
Book author : 
Book subject : Go 語言教程
Book book_id : 6495407
Book title : Python 教程
Book author : 
Book subject : Python 語言教程
Book book_id : 6495700

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

你可以定義指向結(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)體指針重寫以上實(shí)例,代碼如下:

實(shí)例

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 描述 */
? ?Book1.title = "Go 語言"
? ?Book1.author = ""
? ?Book1.subject = "Go 語言教程"
? ?Book1.book_id = 6495407

? ?/* book 2 描述 */
? ?Book2.title = "Python 教程"
? ?Book2.author = ""
? ?Book2.subject = "Python 語言教程"
? ?Book2.book_id = 6495700

? ?/* 打印 Book1 信息 */
? ?printBook(&Book1)

? ?/* 打印 Book2 信息 */
? ?printBook(&Book2)
}
func printBook( book *Books ) {
? ?fmt.Printf( "Book title : %sn", book.title)
? ?fmt.Printf( "Book author : %sn", book.author)
? ?fmt.Printf( "Book subject : %sn", book.subject)
? ?fmt.Printf( "Book book_id : %dn", book.book_id)
}

以上實(shí)例執(zhí)行運(yùn)行結(jié)果為:

Book title : Go 語言
Book author : 
Book subject : Go 語言教程
Book book_id : 6495407
Book title : Python 教程
Book author : 
Book subject : Python 語言教程
Book book_id : 6495700