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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] 接口值可能賦值為任意類型的值,那我們如何從接口值獲取其存儲的具體數據呢? 我們可以借助標準庫fmt包的格式化打印獲取到接口值的動態類型。示例如下 ```go type Mover interface { Move() } type Cat struct{} func (c Cat) Move() { fmt.Println("狗在跑~") } func main() { var m Mover m = Cat{} fmt.Printf("m type is: %T\n", m) } // 運行結果 // m type is: main.Cat ``` 而fmt包內部其實是使用反射的機制在程序運行時獲取到動態類型的名稱。關于反射的內容我們會在后續章節詳細介紹。 而想要從接口值中獲取到對應的實際值需要使用類型斷言,其語法格式如下。 ```go 接口變量.(猜測的類型) ``` >[info] **說明**: 該語法返回兩個參數,第一個參數是接口變量轉化為猜測類型后的變量,第二個值是一個布爾值,若為true則表示斷言成功,為false則表示斷言失敗。 舉例說明 ```go func typeAssertion(x interface{}) { // 沒了解這種為什么可以使用switch..case語法, 可能是搭配使用的... switch v := x.(type) { case string: fmt.Printf("x is a string is %v\n", v) case int: fmt.Printf("x is a int is %v\n", v) case bool: fmt.Printf("x is a bool is %v\n", v) case Cat: fmt.Printf("x is a Cat is %v\n", v) default: fmt.Println("unsupport type") } } func main() { typeAssertion(1) typeAssertion(string("12.32")) typeAssertion(true) typeAssertion([]string{}) typeAssertion(m) } // 運行結果: // x is a int is 1 // x is a string is 12.32 // x is a bool is true // unsupport type // x is a Cat is {} ``` 由于接口類型變量能夠動態存儲不同類型值的特點,所以很多初學者會濫用接口類型(特別是空接口)來實現編碼過程中的便捷。只有當有兩個或兩個以上的具體類型必須以相同的方式進行處理時才需要定義接口。切記不要為了使用接口類型而增加不必要的抽象,導致不必要的運行時損耗。 在 Go 語言中接口是一個非常重要的概念和特性,使用接口類型能夠實現代碼的抽象和解耦,也可以隱藏某個功能的內部實現,但是缺點就是在查看源碼的時候,不太方便查找到具體實現接口的類型。
                  <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>

                              哎呀哎呀视频在线观看