1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        Go1.17這個(gè)新特性竟然是6年前提出來的

        共 4222字,需瀏覽 9分鐘

         ·

        2021-06-01 14:18

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

        大家好,我是 polarisxu。

        Go1.17 預(yù)計(jì)在 8 月份發(fā)布。目前 tip.golang.org 可以瀏覽 Go1.17 的相關(guān)內(nèi)容,https://tip.golang.org/doc/go1.17 也有了 Go1.17 相關(guān)改動(dòng)的部分文檔。這段時(shí)間,我會(huì)陸續(xù)給大家分享 Go1.17 中相關(guān)的新特性,提前學(xué)習(xí)。。。好吧,提前卷了~

        今天先聊聊在測試中增加的隨機(jī)化 flag:shuffle。

        01 安裝 tip 版本

        由于 Go1.17 還未發(fā)布,因此為了體驗(yàn)它的新特性,我們需要安裝 tip 版本。這是一個(gè)正在開發(fā)的版本,也就是倉庫的 master 分支代碼。因此,我們需要通過源碼編譯安裝。

        這里我使用 goup 這個(gè)管理工具進(jìn)行安裝:

        $ goup install tip

        安裝成功后,查看版本信息(你看到的大概率和我的不一樣):

        $ go version
        go version devel go1.17-1607c28172 Sun May 30 02:37:38 2021 +0000 darwin/amd64

        02 新的 shuffle flag

        安裝完 tip 版本后,執(zhí)行如下命令:

        $ go help testflag

        然后找到下面這個(gè) flag:

        -shuffle off,on,N
        Randomize the execution order of tests and benchmarks.
        It is off by default. If -shuffle is set to on, then it will seed
        the randomizer using the system clock. If -shuffle is set to an
        integer N, then N will be used as the seed value. In both cases,
        the seed will be reported for reproducibility.

        這是 Go1.17 新增的,提交的代碼見:https://golang.org/cl/310033。

        從名稱可以看出,這是控制測試執(zhí)行順序是否隨機(jī)的 flag。它有三個(gè)值:off、on 和 N,其中默認(rèn)是 off,即不啟用隨機(jī),這相當(dāng)于 Go1.17 版本之前的測試行為。而 on 表示啟用 shuffle,那 N 是什么意思?它也表示啟用隨機(jī)。on 和 N 的區(qū)別解釋下:

        因?yàn)槭请S機(jī),就涉及到隨機(jī)種子(seed)問題。當(dāng)值是 on 時(shí),隨機(jī)數(shù)種子使用系統(tǒng)時(shí)鐘;如果值是 N,則直接用這個(gè) N 當(dāng)做隨機(jī)數(shù)種子。注意 N 是整數(shù)。

        當(dāng)測試失敗時(shí),如果啟用了 shuffle,這個(gè)種子會(huì)打印出來,方便你重現(xiàn)之前測試場景。

        03 例子體驗(yàn)下

        創(chuàng)建一個(gè)包 calc,增加「加減乘除」四個(gè)函數(shù):

        func Add(x, y int) int {
         return x + y
        }

        func Minus(x, y int) int {
         return x - y
        }

        func Mul(x, y int) int {
         return x * y
        }

        func Div(x, y int) int {
         return x / y
        }

        并為這四個(gè)函數(shù)寫好單元測試(代碼太長,這里只列出 Add 的,寫法不重要,按你喜歡的方式寫單元測試即可):

        func TestAdd(t *testing.T) {
         type args struct {
          x int
          y int
         }
         tests := []struct {
          args args
          want int
         }{
          {
           args{12},
           3,
          },
          {
           args{-13},
           3,  // 特意構(gòu)造一個(gè) failure 的 case
          },
         }
         for _, tt := range tests {
          if got := Add(tt.args.x, tt.args.y); got != tt.want {
           t.Errorf("Add() = %v, want %v", got, tt.want)
          }
         }
        }

        然后運(yùn)行單元測試(不加 shuffle flag):

        $ go test -v ./...
        === RUN   TestAdd
            calc_test.go:27: Add() = 2, want 3
        --- FAIL: TestAdd (0.00s)
        === RUN   TestMinus
        --- PASS: TestMinus (0.00s)
        === RUN   TestMul
        --- PASS: TestMul (0.00s)
        === RUN   TestDiv
        --- PASS: TestDiv (0.00s)
        FAIL
        FAIL test/shuffle 0.441s
        FAIL

        多次運(yùn)行,發(fā)現(xiàn)執(zhí)行順序都是你文件中寫好的單元測試順序,我這里是 Add、Minus、Mul、Div。

        加上 shuffle flag 后運(yùn)行:

        $ go test -v -shuffle=on ./...
        -test.shuffle 1622383890431866000
        === RUN   TestMul
        --- PASS: TestMul (0.00s)
        === RUN   TestDiv
        --- PASS: TestDiv (0.00s)
        === RUN   TestAdd
            calc_test.go:27: Add() = 2, want 3
        --- FAIL: TestAdd (0.00s)
        === RUN   TestMinus
        --- PASS: TestMinus (0.00s)
        FAIL
        FAIL test/shuffle 0.177s
        FAIL

        輸出有兩處變化:

        • 多了 -test.shuffle 1622383890431866000,即上面說到的種子。如果不是 on 而是 N,則這里的值就是 N 的值;
        • 順序不確定。你多次運(yùn)行,發(fā)現(xiàn)每次順序可能不一樣;

        順便提一句,對于 benchmark,shuffle 這個(gè) flag 也是適用的。

        04 有什么用

        有人可能會(huì)問,這個(gè)玩意有啥用?

        確實(shí),大部分時(shí)候這個(gè)特性沒啥用。但如果你不希望測試之間有依賴關(guān)系,而擔(dān)心實(shí)際上依賴了,可以加上這個(gè) flag,以便發(fā)現(xiàn)潛在的問題。

        其實(shí),這個(gè) flag 早在 2015 年 bradfitz 就提 issue 建議加上,原計(jì)劃在 Go1.6 加上的,但沒有人寫提案,因此擱置了。6 年過去了,才加上該功能,可見需求不強(qiáng)烈。日常工作中,你大概率也不會(huì)用到,但知曉有這么個(gè)東西還是有用處的,萬一需要時(shí),可以用上。




        往期推薦


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


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

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

        手機(jī)掃一掃分享

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

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            ass裸体日本少妇pics | 青青草视频播放器 | 欧洲一级毛片AV | 全肉乱妇杂乱小说txt | 久久精品国产免费久久 | 三级国产在线观看 | 青青草官网 | 一边舌吻一边揉我的胸 | 日韩一级A片 | 偷拍自拍一区 |