Install Go on Linux
In order to install go on Linux, copy, paste, and execute the following command:
curl -s https://scripts.programster.org/scripts/15?output=raw | bash
... or copy, paste, and execute the following script:
#!/bin/bash if ! [ -n "$BASH_VERSION" ];then echo "this is not bash, calling self with bash...."; SCRIPT=$(readlink -f "$0") /bin/bash $SCRIPT exit; fi cd $HOME # Download and extract Go for linux VERSION="1.7" curl -O https://storage.googleapis.com/golang/go$VERSION.linux-amd64.tar.gz tar --extract --gzip --file go$VERSION.linux-amd64.tar.gz # /usr/local/go is the officially-recommended location sudo chown -R root:root ./go sudo mv go /usr/local # Create the local directory for go projects mkdir -p $HOME/work/src # Set up go paths echo 'export GOPATH=$HOME/work' >> ~/.profile echo 'export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin' >> ~/.profile
You will need to log out and in again, or run source $HOME/.profile
in order to start using the go commands.
All your go projects need to be in folders under $HOME/work/src
in order to be compiled easily.
HelloWorld
Lets create, build, and execute a hello world application. First lets create our project folder:
mkdir -p $HOME/work/src/hello
Now create our source code:
editor $HOME/work/src/hello/hello.go
Fill it with the following contents:
package main import "fmt" func main() { fmt.Printf("hello, world\n") }
Compile and install the application:
go install hello
Execute the application:
hello
You should get the output:
hello, world
The hello
application (along with any others you build) will be located under: $HOME/work/bin/
which you will see by executing:
which hello
References
First published: 16th August 2018