How do I SET the GOPATH environment variable on Ubuntu? What file must I edit?

前端 未结 25 2800
死守一世寂寞
死守一世寂寞 2020-11-28 00:45

I am trying to do a go get:

go get github.com/go-sql-driver/mysql

and it fails with the following error:

packa         


        
相关标签:
25条回答
  • 2020-11-28 01:12

    go path could be every where you want just create a directory and set global path variable in the name of GOPATH to your environment.

    mkdir ~/go
    export GOPATH=~/go
    go get github.com/go-sql-driver/mysql
    
    0 讨论(0)
  • 2020-11-28 01:14
    export GOPATH=/path/desired/here
    

    There is no need to edit any file, we can just use the command above to directly edit the Go environment variables.

    0 讨论(0)
  • 2020-11-28 01:16

    Write this code in Terminal.

    export GOPATH=path/to/your/gopath/directory
    

    Note: This will reset on every new Terminal window or system restart.

    To be persistent, paste the code below in your .zshrc or .bashrc file according to your shell. Those files in your Home Directory. It will be like below.

    export PATH=path/to/some/other/place/composer/for/example
    export GOPATH=path/to/your/gopath/directory
    export PATH=$PATH:$GOPATH/bin
    
    0 讨论(0)
  • 2020-11-28 01:16

    If you've setup anything that needs modification of environment variables e.g. Java, Go etc this will be very familiar.

    I will assume that you have the following directory structure somewhere as your Go path:

    \---[folder name]
        +---bin
        +---pkg
        \---src
    
    • open a new terminal
    • type sudo nano /etc/environment
    • find PATH=... and go the end of it and add a colon : after the last path then paste in your full go path e.g. /home/user/gocode

    and you're done, This should make it persistent through the system.

    0 讨论(0)
  • 2020-11-28 01:17

    Ubuntu 14.04

    export GOPATH=$HOME/go
    

    Additionally you can add this string to file

    $HOME/.bashrc
    
    0 讨论(0)
  • 2020-11-28 01:18

    New Way: Go Modules

    Since Go 1.11, you don't have to use GOPATH anymore. Simply go to your project directory and do this once:

    go mod init github.com/youruser/yourrepo
    
    • With this, Go creates a module root at that directory.
    • You can create as many modules as you want.
    • For step by step instructions, also see this answer.

    Old Way: GOPATH

    If you insist on working with GOPATH then heed this:

    • Since Go 1.8, you don't need to set your GOPATH or GOROOT.
    • GOPATH by default is under your user/home directory.

    From the documentation:

    If no GOPATH is set, it is assumed to be $HOME/go on Unix systems and %USERPROFILE%\go on Windows. If you want to use a custom location as your workspace, you can set the GOPATH environment variable.

    0 讨论(0)
提交回复
热议问题