kratos 的时候执行 go generate ./... 时出现报错:

C:\Users\Chenz\go\pkg\mod\github.com\google\wire@v0.5.0\cmd\wire\main.go:34:2: missing go.sum entry for module providing package github.com/google/subcommands (imported by github.com/google/wire/cmd/w
ire); to add:
        go get github.com/google/wire/cmd/wire@v0.5.0
C:\Users\Chenz\go\pkg\mod\github.com\google\wire@v0.5.0\internal\wire\copyast.go:21:2: missing go.sum entry for module providing package golang.org/x/tools/go/ast/astutil (imported by github.com/googl
e/wire/internal/wire); to add:
        go get github.com/google/wire/internal/wire@v0.5.0
C:\Users\Chenz\go\pkg\mod\github.com\google\wire@v0.5.0\internal\wire\parse.go:30:2: missing go.sum entry for module providing package golang.org/x/tools/go/packages (imported by github.com/google/wir
e/internal/wire); to add:
        go get github.com/google/wire/internal/wire@v0.5.0
C:\Users\Chenz\go\pkg\mod\github.com\google\wire@v0.5.0\internal\wire\analyze.go:26:2: missing go.sum entry for module providing package golang.org/x/tools/go/types/typeutil (imported by github.com/go
ogle/wire/cmd/wire); to add:
        go get github.com/google/wire/cmd/wire@v0.5.0
app\user\cmd\user\wire_gen.go:3: running "go": exit status 1
C:\Users\Chenz\go\pkg\mod\github.com\google\wire@v0.5.0\cmd\wire\main.go:34:2: missing go.sum entry for module providing package github.com/google/subcommands (imported by github.com/google/wire/cmd/w
ire); to add:
        go get github.com/google/wire/cmd/wire@v0.5.0
C:\Users\Chenz\go\pkg\mod\github.com\google\wire@v0.5.0\internal\wire\copyast.go:21:2: missing go.sum entry for module providing package golang.org/x/tools/go/ast/astutil (imported by github.com/googl
e/wire/internal/wire); to add:
        go get github.com/google/wire/internal/wire@v0.5.0
C:\Users\Chenz\go\pkg\mod\github.com\google\wire@v0.5.0\internal\wire\parse.go:30:2: missing go.sum entry for module providing package golang.org/x/tools/go/packages (imported by github.com/google/wir
e/internal/wire); to add:
        go get github.com/google/wire/internal/wire@v0.5.0
C:\Users\Chenz\go\pkg\mod\github.com\google\wire@v0.5.0\internal\wire\analyze.go:26:2: missing go.sum entry for module providing package golang.org/x/tools/go/types/typeutil (imported by github.com/go
ogle/wire/cmd/wire); to add:
        go get github.com/google/wire/cmd/wire@v0.5.0
cmd\helloworld\wire_gen.go:3: running "go": exit status 1

搜了一下,应该在 idea(或者 goland)的 settings->go module 里加一个参数 GOFLAGS=-mod=mod,这会让 go 在执行命令时自动更新 go.mod 文件。例如,当一个已知没有任何模块提供的包被导入的时候。(有点拗口哈)

具体看官方文档

Build commands
All commands that load information about packages are module-aware. This includes:

  • go build
  • go fix
  • go generate
  • go get
  • go install
  • go list
  • go run
  • go test
  • go vet

When run in module-aware mode, these commands use go.mod files to interpret import paths listed on the command line or written in Go source files. These commands accept the following flags, common to all module commands.

  • The -mod flag controls whether go.mod may be automatically updated and whether the vendor directory is used.
    • -mod=mod tells the go command to ignore the vendor directory and to automatically update go.mod, for example, when an imported package is not provided by any known module.
    • -mod=readonly tells the go command to ignore the vendor directory and to report an error if go.mod needs to be updated.
    • -mod=vendor tells the go command to use the vendor directory. In this mode, the go command will not use the network or the module cache.
  • By default, if the go version in go.mod is 1.14 or higher and a vendor directory is present, the go command acts as if -mod=vendor were used. Otherwise, the go command acts as if -mod=readonly were used.
  • The -modcacherw flag instructs the go command to create new directories in the module cache with read-write permissions instead of making them read-only. When this flag is used consistently (typically by setting GOFLAGS=-modcacherw in the environment or by running go env -w GOFLAGS=-modcacherw), the module cache may be deleted with commands like rm -r without changing permissions first. The go clean -modcache command may be used to delete the module cache, whether or not -modcacherw was used.
  • The -modfile=file.mod flag instructs the go command to read (and possibly write) an alternate file instead of go.mod in the module root directory. The file’s name must end with .mod. A file named go.mod must still be present in order to determine the module root directory, but it is not accessed. When -modfile is specified, an alternate go.sum file is also used: its path is derived from the -modfile flag by trimming the .mod extension and appending .sum.

说起来今天是程序员节。

2022-10-24