<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之旅 廣告
                [TOC] 參考鏈接:[https://juejin.im/post/5ad1c766518825555e5e4646](https://juejin.im/post/5ad1c766518825555e5e4646) ## interface{}之對象轉型坑 一個語言用的久了,難免使用者的思維會受到這個語言的影響,interface{}作為Go的重要特性之一,它代表的是一個類似`*void`的指針,可以指向不同類型的數據。所以我們可以使用它來指向任何數據,這會帶來類似與動態語言的便利性,如以下的例子: ``` type BaseQuestion struct{ QuestionId int QuestionContent string } type ChoiceQuestion struct{ BaseQuestion Options []string } type BlankQuestion struct{ BaseQuestion Blank string } func fetchQuestion(id int) (interface{} , bool) { data1 ,ok1 := fetchFromChoiceTable(id) // 根據ID到選擇題表中找題目,返回(ChoiceQuestion) data2 ,ok2 := fetchFromBlankTable(id) // 根據ID到填空題表中找題目,返回(BlankQuestion) if ok1 { return data1,ok1 } if ok2 { return data2,ok2 } return nil ,false } ``` 在上面的代碼中,data1是`ChoiceQuestion`類型,data2是`BlankQuestion`類型。因此,我們的interface{}指代了三種類型,分別是`ChoiceQuestion`、`BlankQuestion`和`nil`,這里就體現了Go和面向對象語言的不同點了,在面向對象語言中,我們本可以這么寫: ``` func fetchQuestion(id int) (BaseQuestion , bool) { ... } ``` 只需要返回基類`BaseQuestion`即可,需要使用子類的方法或者字段只需要向下轉型。然而在Go中,并沒有這種`is-A`的概念,代碼會無情的提示你,返回值類型不匹配。 ## interface{}之`nil`坑 ``` Under the covers, interfaces are implemented as two elements, a type and a value. The value, called the interface's dynamic value, is an arbitrary concrete value and the type is that of the value. For the int value 3, an interface value contains, schematically, (int, 3). 在幕后,接口被實現為兩個元素,一個類型和一個值。該值稱為接口的動態值,是一個任意的具體值,類型是該值的類型。對于int值3,一個接口值按模式包含(int, 3)。 ``` 參考鏈接:[https://www.jianshu.com/p/a5bc8add7c6e](https://www.jianshu.com/p/a5bc8add7c6e) ## golang的struct里面嵌入interface
                  <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>

                              哎呀哎呀视频在线观看