<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Golang 中一個 time.Duration 相關(guān)類型轉(zhuǎn)換問題

          共 1496字,需瀏覽 3分鐘

           ·

          2020-08-26 13:09

          點(diǎn)擊上方藍(lán)色“Go語言中文網(wǎng)”關(guān)注我們,領(lǐng)全套Go資料,每天學(xué)習(xí)?Go?語言

          int (const 和非 const) 轉(zhuǎn)換為 time.Duration

          這是一個常見的操作,但是我們會遇到以下代碼中的問題,甚是疑惑~

          • 問題
          package?main
          import?(
          ????"fmt"
          ????"reflect"
          ????"time"
          )
          const?a?=?1
          func?test(a?time.Duration)?{
          ????fmt.Printf("%v?\n",?a)
          }
          func?main()?{
          ????b?:=?a?+?1
          ????fmt.Printf("----?a?=?%v?\n",?reflect.TypeOf(a))
          ????test(a)
          ????fmt.Printf("----?b?=?%v?\n",?reflect.TypeOf(b))
          ????//?test(b)??//>?打開注釋后,?會編譯不通過
          }
          • 原因
          Assignability
          ????A?value?x?is?assignable?to?a?variable?of?type?T?("x?is?assignable?to?T")?if?one?of?the?following?conditions?applies:
          ??????-?x's?type?is?identical?to?T.
          ??????-?x'
          s?type?V?and?T?have?identical?underlying?types?and?at?least?one?of?V?or?T?is?not?a?defined?type.
          ??????-?T?is?an?interface?type?and?x?implements?T.
          ??????-?x?is?a?bidirectional?channel?value,?T?is?a?channel?type,?x's?type?V?and?T?have?identical?element?types,?and?at?least?one?of?V?or?T?is?not?a?defined?type.
          ??????-?x?is?the?predeclared?identifier?nil?and?T?is?a?pointer,?function,?slice,?map,?channel,?or?interface?type.
          ??????-?x?is?an?untyped?constant?representable?by?a?value?of?type?T.??

          就是說,在 const 修飾的變量如果沒有明確指明類型時,會根據(jù)需要做類型轉(zhuǎn)換; 而非 const 修飾的變量不會。這就可以解釋上述代碼中的疑惑了。

          • reference: https://hao.studygolang.com/golang_spec.html#id273

          原文作者:joyousx

          原文鏈接:https://www.jianshu.com/p/be3a9b901cb8



          推薦閱讀


          學(xué)習(xí)交流 Go 語言,掃碼回復(fù)「進(jìn)群」即可


          站長 polarisxu

          自己的原創(chuàng)文章

          不限于 Go 技術(shù)

          職場和創(chuàng)業(yè)經(jīng)驗(yàn)


          Go語言中文網(wǎng)

          每天為你

          分享 Go 知識

          Go愛好者值得關(guān)注


          瀏覽 71
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  人人操人人爱人人射 | www.99热这里只有精品 www国产夜插内射视频网站 | 国产毛片精品一区二区色欲黄A片 | 日本成人三级片在线观看网站 | 大黄网站在线观看 |