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

          go test 的一個小技巧:禁用緩存

          共 1339字,需瀏覽 3分鐘

           ·

          2022-05-27 19:19

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

          大家好,我是 polarisxu。

          每次執(zhí)行單元測試,如果代碼沒有任何變動(包括可能讀取的配置文件),則后續(xù)的執(zhí)行,會直接讀取緩存中的測試結(jié)果,同時會有一個 cached 標(biāo)記。

          $?go?test?-run?^TestPrint$?test/hello
          ok???test/hello?0.113s

          $?go?test?-run?^TestPrint$?test/hello
          ok???test/hello?(cached)

          有時候可能希望能執(zhí)行實際的測試,比如看日志輸出,想要禁用緩存,怎么辦?

          碰到這樣的問題,你一般會怎么獲得答案?

          本文介紹幾種方法,對你解決其他問題可能也會有幫助。

          1、查看 go test 的幫助文檔

          $?go?help?testflag

          文檔內(nèi)容很多,初步瀏覽,似乎沒有這樣的 flag。實際上有這么一段話:

          When?'go?test'?runs?in?package?list?mode,?'go?test'?caches?successful
          package?test?results?to?avoid?unnecessary?repeated?running?of?tests.?To
          disable?test?caching,?use?any?test?flag?or?argument?other?than?the
          cacheable?flags.?The?idiomatic?way?to?disable?test?caching?explicitly
          is?to?use?-count=1.

          也就是說加上 -count=1 即可以禁用緩存。

          2、在 go 官方倉庫的 issue 中搜索

          search in github issue

          小提示:因為 issue 比較多,可以考慮加上 in:title 限定只在標(biāo)題中搜索

          其中 https://github.com/golang/go/issues/24573 里面有人提到,在 Go1.10 及以前通過 GOCACHE=off go test 禁用,Go1.11 及以上版本使用 -count=1 flag 禁用。因為 Go1.11 開始,GOCACHE=off 會影響 go.mod。

          同時,在這個 issue 中,還有人提到 VSCode 中如何配置,即加上如下配置:

          "go.testFlags":?["-count=1"]

          3、百度或谷歌搜索

          這是很多人可能會用的方案,當(dāng)然,還包括在 StackOverflow、golang-nuts 郵件組上搜索。


          學(xué)到了一個知識點,是不是還學(xué)到了如何找問題的答案?!授人以漁很重要。




          往期推薦


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


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


          瀏覽 29
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <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>
                  一区二区三区永久免费 | 天天撸天天色 | 亚洲ww | 2025最新操逼视频 | 好逼天天看 |