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

          揚(yáng)眉吐氣:剛剛,Go 已經(jīng)默認(rèn)支持泛型了

          共 3148字,需瀏覽 7分鐘

           ·

          2021-08-27 16:40

          閱讀本文大概需要 5 分鐘。

          大家好,我是 polarisxu。

          上周 Go 1.17 發(fā)布,在發(fā)布文檔中,沒(méi)有提到泛型的事情。但在發(fā)布之前,大家就發(fā)現(xiàn),泛型的相關(guān)代碼在 1.17 中埋下了,只是默認(rèn)不啟用。

          就在最近,gotip 已經(jīng)默認(rèn)啟用泛型。Go 泛型官方叫做:type parameters。

          -G 的默認(rèn)值由 0 改為了 3,那 Go 1.17 有沒(méi)有這個(gè) flag?

          01 Go1.17 啟用泛型支持

          安裝 Go1.17,推薦使用類似 goup 這樣的工具,方便切換版本,確保已經(jīng)是 1.17:

          $ go version
          go version go1.17 darwin/amd64

          然后執(zhí)行如下命令,查看編譯器 flag:

          $ go tool compile -h
          usage: compile [options] file.go...
            -% int
               debug non-static initializers
            -+ compiling runtime
            -B disable bounds checking
            -C disable printing of columns in error messages
            -D path
               set relative path for local imports
            -E debug symbol export
            -G accept generic code
            -I directory
               add directory to import search path
            ....

          注意到:-G accept generic code 了吧,通過(guò)它來(lái)控制是否接受泛型代碼,這個(gè) flag 可能是臨時(shí)性的。從這個(gè)幫助文檔上沒(méi)有看到 -G 能夠接受那些值,通過(guò)查代碼可以知曉(包括上面提到默認(rèn)值由 0 改為 3),0 表示不啟用泛型,3 表示啟用泛型。

          看一個(gè)具體泛型的例子:

          package main

          import (
              "fmt"
          )

          type Addable interface {
           type intint8int16int32int64,
            uintuint8uint16uint32uint64uintptr,
            float32float64complex64complex128,
            string
          }

          func add[T Addable](a, b T) T {
              return a + b
          }

          func main() {
              fmt.Println(add(1,2))

              fmt.Println(add("foo","bar"))
          }

          例子來(lái)自:https://github.com/mattn/go-generics-example。

          使用 Go1.17 編譯:

          $ go build test.go
          # command-line-arguments
          ./test.go:8:2: syntax error: unexpected type, expecting method or interface name
          ./test.go:14:6: missing function body
          ./test.go:14:9: syntax error: unexpected [, expecting (

          發(fā)現(xiàn)報(bào)語(yǔ)法錯(cuò)誤。

          改成這樣編譯:

          $ go build -gcflags=-G=3 test.go

          一切正常,運(yùn)行也得到了正確的結(jié)果。表明 Go1.17 支持泛型了。

          02 gotip 默認(rèn)支持泛型

          上面提到,目前 gotip 已經(jīng)將 -G 默認(rèn)值改為了 3,也就是默認(rèn)就支持泛型了。(gotip 即是 go 倉(cāng)庫(kù)的 master 分支代碼)

          基于上面的代碼用 gotip 試驗(yàn)下,確保版本:

          $ go version
          go version devel go1.18-6416bde023 Sun Aug 22 13:54:24 2021 +0000 darwin/amd64

          直接運(yùn)行上面的代碼:

          $ go run add.go
          3
          foobar

          關(guān)于默認(rèn)啟用泛型 HN 上有很多討論:https://news.ycombinator.com/item?id=28253692,有認(rèn)為泛型來(lái)的太晚的,有說(shuō) Go 根本不需要泛型的。

          我覺(jué)得,如果你不喜歡泛型,完全可以不用嘛。很多時(shí)候,泛型還是很有好處的。相關(guān)泛型的教程,后續(xù)可以慢慢出了。

          03 總結(jié)

          Go 1.17 雖然支持了泛型,但不建議線上使用,畢竟官方承諾的是 Go1.18 正式啟用泛型。而且,有了泛型,需要更多相關(guān)工具、標(biāo)準(zhǔn)庫(kù)的支持,才能夠用起來(lái)更順手。Go1.18,2022 年 2 月見(jiàn)!




          往期推薦


          我是 polarisxu,北大碩士畢業(yè),曾在 360 等知名互聯(lián)網(wǎng)公司工作,10多年技術(shù)研發(fā)與架構(gòu)經(jīng)驗(yàn)!2012 年接觸 Go 語(yǔ)言并創(chuàng)建了 Go 語(yǔ)言中文網(wǎng)!著有《Go語(yǔ)言編程之旅》、開(kāi)源圖書《Go語(yǔ)言標(biāo)準(zhǔn)庫(kù)》等。


          堅(jiān)持輸出技術(shù)(包括 Go、Rust 等技術(shù))、職場(chǎng)心得和創(chuàng)業(yè)感悟!歡迎關(guān)注「polarisxu」一起成長(zhǎng)!也歡迎加我微信好友交流:gopherstudio


          瀏覽 54
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

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

          手機(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.91一区二区 | 日本乱伦性爱 | 云南久久久久视频 | 日本黄色影院在线 |