简体   繁体   English

“GOFLAGS=-mod=mod”这个命令有什么作用?

[英]What does this command do 'GOFLAGS=-mod=mod'?

I am trying to make a Taskfile.yml file for building go application, but I can't quite understand the need of "GOFLAGS=-mod=mod" command before go build main.go.我正在尝试制作一个用于构建 go 应用程序的 Taskfile.yml 文件,但我不太明白在 go build main.go 之前需要“GOFLAGS=-mod=mod”命令。

reference: https://dev.to/aurelievache/learning-go-by-examples-part-3-create-a-cli-app-in-go-1h43参考: https://dev.to/aurelievache/learning-go-by-examples-part-3-create-a-cli-app-in-go-1h43

So there are two things here所以这里有两件事

  • GOFLAGS
    • this is nothing but an environment variable(if you don't understand what an environment variable is, think of it like a value that can be accessed by any process in your current environment. These values are maintained by the OS).这不过是一个环境变量(如果您不了解什么是环境变量,请将其视为当前环境中任何进程都可以访问的值。这些值由操作系统维护)。
    • So this GOFLAGS variable has a space separated list of flags that will automatically be passed to the appropriate go commands.所以这个GOFLAGS变量有一个空格分隔的标志列表,这些标志将自动传递给适当的 go 命令。
    • The flag we are setting here is mod , this flag is applicable to the go build command and may not be applicable to other go commands.我们在这里设置的标志是mod ,这个标志适用于go build命令,可能不适用于其他 go 命令。
    • If you are curious how go does this, refer to this change request如果您好奇go是如何做到这一点的,请参阅此更改请求
    • Since we are mentioning this as part of the command, this environment variable is temporarily set and is not actually exported.由于我们将其作为命令的一部分提及,因此该环境变量是临时设置的,实际上并未导出。
  • what does setting -mod=mod flag, actually do during go build ?设置-mod=mod标志在go build期间实际上做了什么?
    • The -mod flag controls whether go.mod may be automatically updated and whether the vendor directory is used. -mod标志控制 go.mod 是否可以自动更新以及是否使用供应商目录。
    • -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=mod告诉 go 命令忽略供应商目录并自动更新 go.mod,例如,当任何已知模块未提供导入的 package 时。
    • Refer this .参考这个

Therefore所以

GOFLAGS="-mod=mod" go build main.go

is equivalent to相当于

go build -mod=mod main.go

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM