日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Go1.17這個(gè)新特性竟然是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)行安裝:

 
 
 
 
  1. $ goup install tip 

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

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

02 新的 shuffle flag

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

 
 
 
 
  1. $ go help testflag 

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

 
 
 
 
  1. -shuffle off,on,N 
  2.         Randomize the execution order of tests and benchmarks. 
  3.         It is off by default. If -shuffle is set to on, then it will seed 
  4.         the randomizer using the system clock. If -shuffle is set to an 
  5.         integer N, then N will be used as the seed value. In both cases, 
  6.         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ù):

 
 
 
 
  1. func Add(x, y int) int { 
  2.  return x + y 
  3.  
  4. func Minus(x, y int) int { 
  5.  return x - y 
  6.  
  7. func Mul(x, y int) int { 
  8.  return x * y 
  9.  
  10. func Div(x, y int) int { 
  11.  return x / y 

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

 
 
 
 
  1. func TestAdd(t *testing.T) { 
  2.  type args struct { 
  3.   x int 
  4.   y int 
  5.  } 
  6.  tests := []struct { 
  7.   args args 
  8.   want int 
  9.  }{ 
  10.   { 
  11.    args{1, 2}, 
  12.    3, 
  13.   }, 
  14.   { 
  15.    args{-1, 3}, 
  16.    3,  // 特意構(gòu)造一個(gè) failure 的 case 
  17.   }, 
  18.  } 
  19.  for _, tt := range tests { 
  20.   if got := Add(tt.args.x, tt.args.y); got != tt.want { 
  21.    t.Errorf("Add() = %v, want %v", got, tt.want) 
  22.   } 
  23.  } 

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

 
 
 
 
  1. $ go test -v ./... 
  2. === RUN   TestAdd 
  3.     calc_test.go:27: Add() = 2, want 3 
  4. --- FAIL: TestAdd (0.00s) 
  5. === RUN   TestMinus 
  6. --- PASS: TestMinus (0.00s) 
  7. === RUN   TestMul 
  8. --- PASS: TestMul (0.00s) 
  9. === RUN   TestDiv 
  10. --- PASS: TestDiv (0.00s) 
  11. FAIL 
  12. FAIL test/shuffle 0.441s 
  13. FAIL 

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

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

 
 
 
 
  1. $ go test -v -shuffle=on ./... 
  2. -test.shuffle 1622383890431866000 
  3. === RUN   TestMul 
  4. --- PASS: TestMul (0.00s) 
  5. === RUN   TestDiv 
  6. --- PASS: TestDiv (0.00s) 
  7. === RUN   TestAdd 
  8.     calc_test.go:27: Add() = 2, want 3 
  9. --- FAIL: TestAdd (0.00s) 
  10. === RUN   TestMinus 
  11. --- PASS: TestMinus (0.00s) 
  12. FAIL 
  13. FAIL test/shuffle 0.177s 
  14. FAIL 

輸出有兩處變化:

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

順便提一句,對(duì)于 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í),可以用上。

本文轉(zhuǎn)載自微信公眾號(hào)「polarisxu」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系polarisxu公眾號(hào)。


網(wǎng)站欄目:Go1.17這個(gè)新特性竟然是6年前提出來的
當(dāng)前路徑:http://m.5511xx.com/article/dheeisd.html