LoginSignup
18
10

More than 3 years have passed since last update.

VSCodeでGo Toolsにビルドタグを渡す方法

Last updated at Posted at 2020-01-18

背景

VSCodeのGo拡張を使用している際、異なるビルドタグを持ったファイルにある変数や関数等を参照しようとするとコード補完が上手く働かずundeclaredになってしまいます。これはLanguage Serverであるgopls1がタグ無しの状態で参照しているため起こる事象なのでタグを渡してあげる必要があります。

lib_util.go
// +build test

package lib

var (
    testUtil = "test util"
)
lib.go
package lib

import (
    "fmt"
)

func p() {
    fmt.Println(testUtil) // undeclared name: testUtil
}

対処法

settings.json に以下のような形で追記してやることでgoplsを含む各種Go Toolsにビルドタグを渡せます。プロジェクト毎に変更したい場合はWorkspaceレベルで設定してやると良いでしょう2

適用後はVSCodeを再起動するかF1メニューから "Developer: Reload Window" する必要があります(私の環境では "Go: Restart Language Server" だと上手くいきませんでした)。

    "go.toolsEnvVars": {
        "GOFLAGS": "-tags=test"
    },

参考文献

18
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
10