<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                結構體定義的一般方式如下: ~~~ type identifier struct { field1 type1 field2 type2 ... } ~~~ `type T struct {a, b int}`?也是合法的語法,它更適用于簡單的結構體。 結構體里的字段都有?**名字**,像 field1、field2 等,如果字段在代碼中從來也不會被用到,那么可以命名它為?**_**。 結構體的字段可以是任何類型,甚至是結構體本身(參考第?[10.5](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/10.5.md)?節),也可以是函數或者接口(參考第 11 章)。可以聲明結構體類型的一個變量,然后像下面這樣給它的字段賦值: ~~~ var s T s.a = 5 s.b = 8 ~~~ 數組可以看作是一種結構體類型,不過它使用下標而不是具名的字段。 **使用 new** 使用?**new**?函數給一個新的結構體變量分配內存,它返回指向已分配內存的指針:`var t *T = new(T)`,如果需要可以把這條語句放在不同的行(比如定義是包范圍的,但是分配卻沒有必要在開始就做)。 ~~~ var t *T t = new(T) ~~~ 寫這條語句的慣用方法是:`t := new(T)`,變量?`t`?是一個指向?`T`的指針,此時結構體字段的值是它們所屬類型的零值。 聲明?`var t T`?也會給?`t`?分配內存,并零值化內存,但是這個時候?`t`?是類型T。在這兩種方式中,`t`?通常被稱做類型 T 的一個實例(instance)或對象(Object)。 示例 10.1?[structs_fields.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_10/structs_fields.go)?給出了一個非常簡單的例子: ~~~ package main import "fmt" type struct1 struct { i1 int f1 float32 str string } func main() { ms := new(struct1) ms.i1 = 10 ms.f1 = 15.5 ms.str= "Chris" fmt.Printf("The int is: %d\n", ms.i1) fmt.Printf("The float is: %f\n", ms.f1) fmt.Printf("The string is: %s\n", ms.str) fmt.Println(ms) } ~~~ 輸出: ~~~ The int is: 10 The float is: 15.500000 The string is: Chris &{10 15.5 Chris} ~~~ 使用?`fmt.Println`?打印一個結構體的默認輸出可以很好的顯示它的內容,類似使用?**%v**?選項。 就像在面向對象語言所作的那樣,可以使用點號符給字段賦值:`structname.fieldname = value`。 同樣的,使用點號符可以獲取結構體字段的值:`structname.fieldname`。 在 Go 語言中這叫?**選擇器(selector)**。無論變量是一個結構體類型還是一個結構體類型指針,都使用同樣的?**選擇器符(selector-notation)**?來引用結構體的字段: ~~~ type myStruct struct { i int } var v myStruct // v是結構體類型變量 var p *myStruct // p是指向一個結構體類型變量的指針 v.i p.i ~~~ 初始化一個結構體實例(一個結構體字面量:struct-literal)的更簡短和慣用的方式如下: ~~~ ms := &struct1{10, 15.5, "Chris"} // 此時ms的類型是 *struct1 ~~~ 或者: ~~~ var mt struct1 ms := struct1{10, 15.5, "Chris"} ~~~ 混合字面量語法(composite literal syntax)`&struct1{a, b, c}`?是一種簡寫,底層仍然會調用?`new ()`,這里值的順序必須按照字段順序來寫。在下面的例子中能看到可以通過在值的前面放上字段名來初始化字段的方式。表達式?`new(Type)`和?`&Type{}`?是等價的。 時間間隔(開始和結束時間以秒為單位)是使用結構體的一個典型例子: ~~~ type Interval struct { start int end int } ~~~ 初始化方式: ~~~ intr := Interval{0, 3} (A) intr := Interval{end:5, start:1} (B) intr := Interval{end:5} (C) ~~~ 在(A)中,值必須以字段在結構體定義時的順序給出,**&**?不是必須的。(B)顯示了另一種方式,字段名加一個冒號放在值的前面,這種情況下值的順序不必一致,并且某些字段還可以被忽略掉,就像(C)中那樣。 結構體類型和字段的命名遵循可見性規則(第?[4.2](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/04.2.md)?節),一個導出的結構體類型中有些字段是導出的,另一些不是,這是可能的。 下圖說明了結構體類型實例和一個指向它的指針的內存布局: ~~~ type Point struct { x, y int } ~~~ 使用 new 初始化: [![](https://github.com/Unknwon/the-way-to-go_ZH_CN/raw/master/eBook/images/10.1_fig10.1-1.jpg?raw=true)](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/images/10.1_fig10.1-1.jpg?raw=true) 作為結構體字面量初始化: [![](https://github.com/Unknwon/the-way-to-go_ZH_CN/raw/master/eBook/images/10.1_fig10.1-2.jpg?raw=true)](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/images/10.1_fig10.1-2.jpg?raw=true) 類型 strcut1 在定義它的包 pack1 中必須是唯一的,它的完全類型名是:`pack1.struct1`。 下面的例子?[Listing 10.2—person.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/person.go)?顯示了一個結構體 Person,一個方法,方法有一個類型為?`*Person`?的參數(因此對象本身是可以被改變的),以及三種調用這個方法的不同方式: ~~~ package main import ( "fmt" "strings" ) type Person struct { firstName string lastName string } func upPerson(p *Person) { p.firstName = strings.ToUpper(p.firstName) p.lastName = strings.ToUpper(p.lastName) } func main() { // 1-struct as a value type: var pers1 Person pers1.firstName = "Chris" pers1.lastName = "Woodward" upPerson(&pers1) fmt.Printf("The name of the person is %s %s\n", pers1.firstName, pers1.lastName) // 2—struct as a pointer: pers2 := new(Person) pers2.firstName = "Chris" pers2.lastName = "Woodward" (*pers2).lastName = "Woodward" // 這是合法的 upPerson(pers2) fmt.Printf("The name of the person is %s %s\n", pers2.firstName, pers2.lastName) // 3—struct as a literal: pers3 := &Person{"Chris","Woodward"} upPerson(pers3) fmt.Printf("The name of the person is %s %s\n", pers3.firstName, pers3.lastName) } ~~~ 輸出: ~~~ The name of the person is CHRIS WOODWARD The name of the person is CHRIS WOODWARD The name of the person is CHRIS WOODWARD ~~~ 在上面例子的第二種情況中,可以直接通過指針,像?`pers2.lastName="Woodward"`?這樣給結構體字段賦值,沒有像 C++ 中那樣需要使用?`->`?操作符,Go 會自動做這樣的轉換。 注意也可以通過解指針的方式來設置值:`(*pers2).lastName = "Woodward"` **結構體的內存布局** Go 語言中,結構體和它所包含的數據在內存中是以連續塊的形式存在的,即使結構體中嵌套有其他的結構體,這在性能上帶來了很大的優勢。不像 Java 中的引用類型,一個對象和它里面包含的對象可能會在不同的內存空間中,這點和 Go 語言中的指針很像。下面的例子清晰地說明了這些情況: ~~~ type Rect1 struct {Min, Max Point } type Rect2 struct {Min, Max *Point } ~~~ [![](https://github.com/Unknwon/the-way-to-go_ZH_CN/raw/master/eBook/images/10.1_fig10.2.jpg?raw=true)](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/images/10.1_fig10.2.jpg?raw=true) **遞歸結構體** 結構體類型可以通過引用自身來定義。這在定義鏈表或二叉樹的元素(通常叫節點)時特別有用,此時節點包含指向臨近節點的鏈接(地址)。如下所示,鏈表中的?`su`,樹中的?`ri`?和?`le`?分別是指向別的節點的指針。 鏈表: [![](https://github.com/Unknwon/the-way-to-go_ZH_CN/raw/master/eBook/images/10.1_fig10.3.jpg?raw=true)](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/images/10.1_fig10.3.jpg?raw=true) 這塊的?`data`?字段用于存放有效數據(比如 float64),`su`?指針指向后繼節點。 Go 代碼: ~~~ type Node struct { data float64 su *Node } ~~~ 鏈表中的第一個元素叫?`head`,它指向第二個元素;最后一個元素叫?`tail`,它沒有后繼元素,所以它的?`su`?為 nil 值。當然真實的鏈接會有很多數據節點,并且鏈表可以動態增長或收縮。 同樣地可以定義一個雙向鏈表,它有一個前趨節點?`pr`?和一個后繼節點?`su`: ~~~ type Node struct { pr *Node data float64 su *Node } ~~~ 二叉樹: [![](https://github.com/Unknwon/the-way-to-go_ZH_CN/raw/master/eBook/images/10.1_fig10.4.jpg?raw=true)](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/images/10.1_fig10.4.jpg?raw=true) 二叉樹中每個節點最多能鏈接至兩個節點:左節點(le)和右節點(ri),這兩個節點本身又可以有左右節點,依次類推。樹的頂層節點叫根節點(**root**),底層沒有子節點的節點叫葉子節點(**leaves**),葉子節點的?`le`?和?`ri`?指針為 nil 值。在 Go 中可以如下定義二叉樹: ~~~ type Tree strcut { le *Tree data float64 ri *Tree } ~~~ **結構體轉換** Go 中的類型轉換遵循嚴格的規則。當為結構體定義了一個 alias 類型時,此結構體類型和它的 alias 類型都有相同的底層類型,它們可以如示例 10.3 那樣互相轉換,同時需要注意其中非法賦值或轉換引起的編譯錯誤。 示例 10.3: ~~~ package main import "fmt" type number struct { f float32 } type nr number // alias type func main() { a := number{5.0} b := nr{5.0} // var i float32 = b // compile-error: cannot use b (type nr) as type float32 in assignment // var i = float32(b) // compile-error: cannot convert b (type nr) to type float32 // var c number = b // compile-error: cannot use b (type nr) as type number in assignment // needs a conversion: var c = number(b) fmt.Println(a, b, c) } ~~~ 輸出: ~~~ {5} {5} {5} ~~~ **練習 10.1**?vcard.go: 定義結構體 Address 和 VCard,后者包含一個人的名字、地址編號、出生日期和圖像,試著選擇正確的數據類型。構建一個自己的 vcard 并打印它的內容。 ~~~ 提示: VCard 必須包含住址,它應該以值類型還是以指針類型放在 VCard 中呢? 第二種會好點,因為它占用內存少。包含一個名字和兩個指向地址的指針的 Address 結構體可以使用 %v 打印: {Kersschot 0x126d2b80 0x126d2be0} ~~~ **練習 10.2**?persionext1.go: 修改 persionext1.go,使它的參數 upPerson 不是一個指針,解釋下二者的區別。 **練習 10.3**?point.go: 使用坐標 X、Y 定義一個二維 Point 結構體。同樣地,對一個三維點使用它的極坐標定義一個 Polar 結構體。實現一個`Abs()`?方法來計算一個 Point 表示的向量的長度,實現一個?`Scale`?方法,它將點的坐標乘以一個尺度因子(提示:使用`math`?包里的?`Sqrt`?函數)(function Scale that multiplies the coordinates of a point with a scale factor)。 **練習 10.3**?rectangle.go: 定義一個 Rectangle 結構體,它的長和寬是 int 類型,并定義方法?`Area()`?和?`Primeter()`,然后進行測試。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看