spf13/cobra can't download binary to $GOPATH/bin

3.4k views Asked by At

I want to use spf13/cobra on my project.

spf13/cobra: A Commander for modern Go CLI interactions

typed install command result

$ go get -u github.com/spf13/cobra/cobra
go: downloading github.com/spf13/cobra v1.0.0
go: downloading github.com/spf13/cobra/cobra v0.0.0-20200826151851-02a0d2fbc9e6
go: github.com/spf13/cobra/cobra upgrade => v0.0.0-20200826151851-02a0d2fbc9e6
go get github.com/spf13/cobra/cobra: ambiguous import: found package github.com/spf13/cobra/cobra in multiple modules:
        github.com/spf13/cobra v1.0.0 (/go/pkg/mod/github.com/spf13/[email protected]/cobra)
        github.com/spf13/cobra/cobra v0.0.0-20200826151851-02a0d2fbc9e6 (/go/pkg/mod/github.com/spf13/cobra/[email protected])

but not found cobra command and binary file.

$ cobra
bash: cobra: command not found
$ ls -la $GOPATH/bin | grep cobra
// not found binary

this is my go env.

$ go env
GO111MODULE="auto"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/vscode/.cache/go-build"
GOENV="/home/vscode/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/workspaces/[my-package-name]/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build068490139=/tmp/go-build -gno-record-gcc-switches"

and develop via container based on microsoft/vscode-remote-try-go.

How can I do install binary to $GOPATH/bin?

3

There are 3 answers

1
just_a_user On

Install the Cobra Generator

go get github.com/spf13/cobra/cobra

More Info: https://github.com/spf13/cobra/blob/master/cobra/README.md

0
cn007b On

Actually go get -u github.com/spf13/cobra/cobra will install cobra into $GOPATH/bin/cobra you can check it by:

ls $GOPATH/bin/cobra

But when you run:

cobra

in your environment must be specified $PATH which leads to $GOPATH/bin, to do that you must run:

export PATH=$PATH:$GOPATH/bin

So now all must be ok.

PS: Add export PATH=$PATH:$GOPATH/bin to your ~/.bash_profile file so it will work after terminal restart.

0
DamarOwen On

for those who face this problems

you need to instal the cobra-cli NOT cobra

go install github.com/spf13/cobra-cli@latest

dont install with command like following

go get -u github.com/spf13/cobra@latest