<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## struct定義 使用type和struct關鍵字來定義結構體,具體代碼格式如下: ~~~ type 類型名 struct { 字段名 字段類型 字段名 字段類型 … } ~~~ 類型名:標識自定義結構體的名稱,在同一個包內不能重復 字段名:表示結構體字段名。結構體中的字段名必須唯一 字段類型:表示結構體字段的具體類型 *說明:同樣類型的字段也可以寫在一行* ### 結構體字段的可見性 結構體中字段大寫開頭表示可公開訪問,小寫表示私有(僅在定義當前結構體的包中可訪問) ### struct實例化 ~~~ stu1 := Student{"jim", 20, 73.5} stu2 := Student{ // 初始化結構體的時候可以簡寫,也就是初始化的時候不寫鍵,直接寫值 name: "Tom", age: 24, weight: 59.3, } stu3 := new(Student) // 注意這里返回的是指針,相當于 stu3 := &Student{} stu3.name = "lisi" ~~~ ## 使用 New() 函數,而非構造器 Go 并不支持構造器。如果某類型的零值不可用,需要程序員來隱藏該類型,避免從其他包直接訪問。程序員應該提供一種名為`NewT(parameters)`的[函數](https://studygolang.com/articles/11892),按照要求來初始化`T`類型的變量。按照 Go 的慣例,應該把創建`T`類型變量的函數命名為`NewT(parameters)`。這就類似于構造器了。如果一個包只含有一種類型,按照 Go 的慣例,應該把函數命名為`New(parameters)`, 而不是`NewT(parameters)` ~~~go package employee import ( "fmt" ) type employee struct { firstName string lastName string totalLeaves int leavesTaken int } func New(firstName string, lastName string, totalLeave int, leavesTaken int) employee { e := employee {firstName, lastName, totalLeave, leavesTaken} return e } func (e employee) LeavesRemaining() { fmt.Printf("%s %s has %d leaves remaining", e.firstName, e.lastName, (e.totalLeaves - e.leavesTaken)) } ~~~ ### 結構體的零值(Zero Value) 當定義好的結構體并沒有被顯式地初始化時,該結構體的字段將默認賦為零值 ### 結構體的指針 創建指向結構體的指針 ~~~go type Employee struct { firstName, lastName string age, salary int } func main() { emp8 := &Employee{"Sam", "Anderson", 55, 6000} fmt.Println("First Name:", (*emp8).firstName) // Go 語言允許我們在訪問 firstName 字段時,可以使用 emp8.firstName 來代替顯式的解引用 (*emp8).firstName fmt.Println("First Name:", emp8.firstName) fmt.Println("Age:", (*emp8).age) } ~~~ ### 導出結構體和字段 如果結構體名稱以大寫字母開頭,則它是其他包可以訪問的導出類型(Exported Type)。同樣,如果結構體里的字段首字母大寫,它也能被其他包訪問到 ~~~go package computer type Spec struct { //exported struct Maker string //exported field model string //unexported field Price int //exported field } ~~~ ### 結構體相等性(Structs Equality) * 結構體是值類型。如果它的每一個字段都是可比較的,則該結構體也是可比較的。如果兩個結構體變量的對應字段相等,則這兩個變量也是相等的; * 如果結構體包含不可比較的字段,則結構體變量也不可比較 ~~~go type image struct { data map[int]int } func main() { image1 := image{data: map[int]int{ 0: 155, }} image2 := image{data: map[int]int{ 0: 155, }} if image1 == image2 { fmt.Println("image1 and image2 are equal") } } output=> invalid operation: image1 == image2 (struct containing map[int]int cannot be compared) ~~~ ### 結構體內存布局 ~~~ type test struct { a int8 b int8 c int8 d int8 } n := test{ 1, 2, 3, 4, } fmt.Printf("n.a %p\n", &n.a) fmt.Printf("n.b %p\n", &n.b) fmt.Printf("n.c %p\n", &n.c) fmt.Printf("n.d %p\n", &n.d) ~~~ 輸出: ~~~ n.a 0xc0000a0060 n.b 0xc0000a0061 n.c 0xc0000a0062 n.d 0xc0000a0063 ~~~
                  <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>

                              哎呀哎呀视频在线观看