LoginSignup
4
3

More than 3 years have passed since last update.

[Go] Cobraをgo getでインストールしようとするとcommand not foundになる問題の解決

Posted at

問題

cobraをインストールしてCLIを書いてみようと思って、cobra公式のインストールコマンドを実行した。

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

その後cobraがインストールされたか確認しようとしたらcommand not foundと表示された。

cobra --help
command not found

ちゃんと$GOROOT$GOPATH$GO111MODULEなどの環境変数の設定もしているので謎だった。

環境

  • MacOS Catalina 10.15.6
  • go version go1.15 darwin/amd64
  • goenv 2.0.0beta11

解決策

よく見るとインストールコマンドを実行した時のメッセージは以下のようになっていた。

go: github.com/spf13/cobra/cobra upgrade => v0.0.0-20200909172742-8a63648dd905
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 (/Users/hoge/go/1.15.0/pkg/mod/github.com/spf13/cobra@v1.0.0/cobra)
    github.com/spf13/cobra/cobra v0.0.0-20200909172742-8a63648dd905 (/Users/hoge/go/1.15.0/pkg/mod/github.com/spf13/cobra/cobra@v0.0.0-20200909172742-8a63648dd905)

これについてググってみるとcobra公式のIssune#1215に解決策があった。

インストールするcobraのバージョンの指定が必要だったらしい。

A workaround I found was to "go get" a specific version:

go get -u github.com/spf13/cobra/cobra@v1.0.0

これでok.

cobra --help
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.

Usage:
  cobra [command]

Available Commands:
  add         Add a command to a Cobra Application
  help        Help about any command
  init        Initialize a Cobra Application

Flags:
  -a, --author string    author name for copyright attribution (default "YOUR NAME")
      --config string    config file (default is $HOME/.cobra.yaml)
  -h, --help             help for cobra
  -l, --license string   name of license for the project
      --viper            use Viper for configuration (default true)

Use "cobra [command] --help" for more information about a command.
4
3
1

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
4
3