前言

go getMac: bash: /Users/libin/go/bin/xxx: No such file or directoryWindows: xxx不是内部或外部命令,也不是可运行的程序或批处理文件

在常用的工具中,拿 protoc-gen-go 和 wire 来举例,需要文档的安装命令是:

go get github.com/google/wire/cmd/wire
go.modgo.mod
localhost:bin libin$ go get github.com/google/wire/cmd/wire
go: go.mod file not found in current directory or any parent directory.
        'go get' is no longer supported outside a module.
        To build and install a command, use 'go install' with a version,
        like 'go install example.com/cmd@latest'
        For more information, see https://golang.org/doc/go-get-install-deprecation
        or run 'go help get' or 'go help install'.
go.mod
localhost:bin libin$ wire
bash: /Users/libin/go/bin/wire: No such file or directory

解决过程

原因分析

在 Go 1.3 之前,由于采用的是 GOPATH 模式,用以上的安装方法当然没问题,因为下载的包,最后会编译为可执行程序后安装到项目的bin目录。

$GOPATH/bin
go env$GOPATH/bin$PATH

解决方案

在任意目录处,使用 install 安装程序

go install github.com/google/wire/cmd/wire@latest
$GOPATH/binwire
localhost:~ libin$ wire help
Usage: wire <flags> <subcommand> <subcommand args>

Subcommands:
        check            print any Wire errors found
        commands         list all command names
        diff             output a diff between existing wire_gen.go files and what gen would generate
        flags            describe all known top-level flags
        gen              generate the wire_gen.go file for each package
        help             describe subcommands and their syntax
        show             describe all top-level provider sets


Use "wire flags" for a list of top-level flags

补充知识

go install 和 go get 的区别

go installgo.modgo installgo get

go get 其他参数介绍

  • -d 只下载不安装
  • -f 只有在你包含了 -u 参数的时候才有效,不让 -u 去验证 import 中的每一个都已经获取了,这对于本地 fork 的包特别有用
  • -fix 在获取源码之后先运行 fix,然后再去做其他的事情
  • -t 同时也下载需要为运行测试所需要的包
  • -u 强制使用网络去更新包和它的依赖包
  • -v 显示执行的命令