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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                > go語言中結構體,就是字段的集合,類似面向對象中的類。 [TOC] ## 定義結構體 > 語法 ~~~ type 結構體名稱 struct { 字段集合 } ~~~ > 如下 ~~~ type Book struct { title string author string } ~~~ ## 結構體初始化 ~~~ // 方式1:按順序初始化字段值 book := Book{"Go 語言", "xk"} // 方式2:指定字段初始化(推薦) book2 := Books{title: "Go 語言", author: "wx"} // 忽略的字段為0 或 空 book3 := Books{title: "Go 語言"} // 字段的讀寫 book2.title = "new book" fmt.Println(book2.title) ~~~ ## 結構體方法 > 結構體可以擁有方法(函數),結構體擁有方法后,結構體就類似面向對象的類。 > 結構體方法的語法: ~~~ func (結構體變量名 結構體類型) 函數名(函數參數列表) (函數返回值列表) { 函數體 } ~~~ > 例子 ~~~ package main import ( "fmt" ) //定義Student結構體 type Student struct { Name string Age int } // 定義Student方法 func (stu *Student) SayHello() string { return "Hello " + stu.Name } func (stu *Student) SetName(name string) { stu.Name = name } func main() { s1 := Student{} s1.Name = "wangkun" s1.SetName("jiaojiao") fmt.Println(s1.SayHello()) } ~~~ ## 結構體指針 ~~~ package main import ( "fmt" ) type Book struct { title string author string } func main() { book := Book{title: "go語言", author: "wk"} // 定義了一個結構體指針 var book_pointer *Book book_pointer = &book // 讀寫結構體指針,假如修改了指針的字段,實際的引用對應的字段也變化 fmt.Println(book_pointer.title) book_pointer.author = "jj" fmt.Println(book) } ~~~ ## 結構體作為函數參數 > 結構體作為函數參數有兩種方式:傳值 和 傳引用 ~~~ package main import ( "fmt" ) type Student struct { name string age int } // 傳值:調用函數時將實際參數復制一份傳遞到函數中,對參數的修改不會影響實際參數 func Test1(s Student) { fmt.Println("名稱:" + s.name) // 非指針的類型引用,只能用來讀取操作 // 當傳入的參數不是指針類型,以下修改操作不生效 s.name = "wk" s.age = 22 } // 傳引用:調用函數時將實際參數的地址直接傳遞到函數中,對參數進行修改,將影響到實際參數。 func Test2(s *Student) { s.name = "wk" s.age = 22 } func main() { student := Student{name: "init", age: 0} Test1(student) fmt.Println(student) //輸出結果:{init 0} Test2(&student) fmt.Println(student) //輸出結果:{wk 22} } ~~~ ## 嵌套結構體 > 結構體字段類型可以是結構體類型 ~~~ package main import "fmt" // 定義UserInfo結構體類型 type UserInfo struct { Id int Username string } // 定義Order結構體 type Order struct { Id int User UserInfo // User字段的類型是UserInfo結構體類型 } func main() { // 初始化結構體 o := Order{} o.Id = 100 // 通過UserInfo結構體初始化User字段 o.User = UserInfo{Id: 1, Username: "wk"} // 打印字段值 fmt.Println(o.Id) fmt.Println(o.User.Id) fmt.Println(o.User.Username) } ~~~
                  <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>

                              哎呀哎呀视频在线观看