Can't get Golang to work in Ubuntu

前端 未结 4 2177
一个人的身影
一个人的身影 2021-02-06 23:38

Ok, So I\'ve downloaded Go 1.1 and put it into $HOME/Documents/go.

Then, I\'ve modified my .bashrc to be:

export GOPATH=$HOME/Documents/go           


        
4条回答
  •  感动是毒
    2021-02-07 00:23

    To install go it is CRITICAL to first remove prior install (or you will get errors go build fails : runtime/mstkbar.go:151:10: debug.gcstackbarrieroff undefined)

    dpkg -l|grep golang  #  if you see any, run following cmd to remove
    sudo apt-get purge golang-*
    

    Verify whether go is still installed

    type go    # see if go is installed
    

    typical output if go is installed

    go is hashed (/usr/local/go/bin/go)
    

    if go is installed remove it

    sudo rm -rf /usr/local/go     #  not just /usr/local/go/bin/go
    

    download the latest tarball https://golang.org/dl/ expand and install

    new_golang_ver=$(curl https://golang.org/VERSION?m=text 2> /dev/null)
    cd /tmp
    wget https://dl.google.com/go/${new_golang_ver}.linux-amd64.tar.gz
    sudo tar -C /usr/local -xzf  ${new_golang_ver}.linux-amd64.tar.gz
    

    define these environment variables in your ~/.bashrc

    export PATH=/usr/local/go/bin:${PATH}
    export GOPATH=${HOME}/gopath  # typical value change at will
    export PATH=${GOPATH}/bin:${PATH}
    

    source your above new env var definitions

    source ~/.bashrc  
    

    verify install

    go version
    

    if installed correctly typical output

    go version go1.12.1 linux/amd64
    

    --- install is complete so lets compile a hello world ---

    [[ ! -d $GOPATH ]] && mkdir -p ${GOPATH}/{bin,pkg,src} # if no dir then create
    mkdir -p ${GOPATH}/src/github.com/mygithubname/play
    cd ${GOPATH}/src/github.com/mygithubname/play
    

    now paste following to create the go source file hello_world.go

    tee hello_world.go  << WOW
    
    package main
    
    import "fmt"
    
    func main() {
        fmt.Printf("hello, world\n")
    }
    
    WOW
    

    compile your source code

    go build hello_world.go 
    
    ./hello_world     #  run your executable 
    

    hello, world


    to understand how to structure your go source code see https://golang.org/doc/code.html

    Fix the missing package error

    You also asked how to fix the missing dependency errors ... for example

    scott@rapacious ~/other_src/gopath/src/github.com/bigeagle/gohop $ go build
    hop/cipher.go:27:2: cannot find package "github.com/golang/snappy" in any of:
        /usr/local/go/src/github.com/golang/snappy (from $GOROOT)
        /home/scott/other_src/gopath/src/github.com/golang/snappy (from $GOPATH)
    internal/logging.go:6:2: cannot find package "github.com/op/go-logging" in any of:
        /usr/local/go/src/github.com/op/go-logging (from $GOROOT)
        /home/scott/other_src/gopath/src/github.com/op/go-logging (from $GOPATH)
    

    above error will happen even after your go install is setup OK ... just issue this to download and install the missing upstream dependency go packages (and the ones they might also reference recursively)

    cd ~/Documents/go/src/github.com/jan/scrypt/ # cd into the source dir
    go get -v -t ./...   #  -v  verbose
                         #  -t  also download any test only packages
    go build
    

提交回复
热议问题