cannot download, $GOPATH not set

前端 未结 15 2179
闹比i
闹比i 2020-11-29 17:00

I want to install json2csv using go get github.com/jehiah/json2csv but I receive this error:

package github.com/jehiah/json2csv: cannot download         


        
15条回答
  •  被撕碎了的回忆
    2020-11-29 17:51

    Using brew

    I installed it using brew.

    $ brew install go
    

    When it was done if you run this brew command it'll show the following info:

    $ brew info go
    go: stable 1.4.2 (bottled), HEAD
    Go programming environment
    https://golang.org
    /usr/local/Cellar/go/1.4.2 (4676 files, 158M) *
      Poured from bottle
    From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/go.rb
    ==> Options
    --with-cc-all
        Build with cross-compilers and runtime support for all supported platforms
    --with-cc-common
        Build with cross-compilers and runtime support for darwin, linux and windows
    --without-cgo
        Build without cgo
    --without-godoc
        godoc will not be installed for you
    --without-vet
        vet will not be installed for you
    --HEAD
        Install HEAD version
    ==> Caveats
    As of go 1.2, a valid GOPATH is required to use the `go get` command:
      https://golang.org/doc/code.html#GOPATH
    
    You may wish to add the GOROOT-based install location to your PATH:
      export PATH=$PATH:/usr/local/opt/go/libexec/bin
    

    The important pieces there are these lines:

    /usr/local/Cellar/go/1.4.2 (4676 files, 158M) *

    export PATH=$PATH:/usr/local/opt/go/libexec/bin

    Setting up GO's environment

    That shows where GO was installed. We need to do the following to setup GO's environment:

    $ export PATH=$PATH:/usr/local/opt/go/libexec/bin
    $ export GOPATH=/usr/local/opt/go/bin
    

    You can then check using GO to see if it's configured properly:

    $ go env
    GOARCH="amd64"
    GOBIN=""
    GOCHAR="6"
    GOEXE=""
    GOHOSTARCH="amd64"
    GOHOSTOS="darwin"
    GOOS="darwin"
    GOPATH="/usr/local/opt/go/bin"
    GORACE=""
    GOROOT="/usr/local/Cellar/go/1.4.2/libexec"
    GOTOOLDIR="/usr/local/Cellar/go/1.4.2/libexec/pkg/tool/darwin_amd64"
    CC="clang"
    GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
    CXX="clang++"
    CGO_ENABLED="1"
    

    Setting up json2csv

    Looks good, so lets install json2csv:

    $ go get github.com/jehiah/json2csv
    $
    

    What just happened? It installed it. You can check like this:

    $ $ ls -l $GOPATH/bin
    total 5248
    -rwxr-xr-x  1 sammingolelli  staff  2686320 Jun  9 12:28 json2csv
    

    OK, so why can't I type json2csv in my shell? That's because the /bin directory under $GOPATH isn't on your $PATH.

    $ type -f json2csv
    -bash: type: json2csv: not found
    

    So let's temporarily add it:

    $ export PATH=$GOPATH/bin:$PATH
    

    And re-check:

    $ type -f json2csv
    json2csv is hashed (/usr/local/opt/go/bin/bin/json2csv)
    

    Now it's there:

    $ json2csv --help
    Usage of json2csv:
      -d=",": delimiter used for output values
      -i="": /path/to/input.json (optional; default is stdin)
      -k=[]: fields to output
      -o="": /path/to/output.json (optional; default is stdout)
      -p=false: prints header to output
      -v=false: verbose output (to stderr)
      -version=false: print version string
    

    Add the modifications we've made to $PATH and $GOPATH to your $HOME/.bash_profile to make them persist between reboots.

提交回复
热议问题