Create Your First GOLang 'Hello World!' Program

First GO Hello World

Please visit this link if you need to learn how to install GOLang on Linux machine.

Now let us create your own first go project, go project consists of main three folder (bin, src and pkg), create them in your desired folder:

cd /home/dotsway/
mkdir -p ~/first_go_project/{bin,src,pkg}

Now add your project folder to the Linux environment if not there yet by adding below to Linux environment.

export GOPATH="$HOME/first_go_project"
export GOBIN="$GOPATH/bin"

Now run source ~/.bashrc to reload it and then type echo $GOPATH to confirm that folder is in the right place:

[root@hmharddcweb01 meliks]# go version
go version go1.8.1 linux/amd64
[root@hmharddcweb01 meliks]# go env
GOARCH="amd64"
GOBIN="/root/first_go_project/bin"
GOEXE=""
GOHOSTARCH="amd64"
[root@hmharddcweb01 meliks]# go help

Writing Your First GOLang Hello World!

First be sure that you installed go on you machine by running 'go version' and have similar output:

go version go1.8.1 linux/amd64

Make a folder and file in your projects folder, to know where is your project folders you can run below command:
echo $GOPATH, below example shows that if GOPATH is /root/first_go_project.

mkdir -p /root/first_go_project/src/hello
vi /root/first_go_project/src/hello/hello.go

Type the following code inside hello.go

package main
import "fmt"
func main() {
fmt.Printf("Hello from dotsway.com, you have created your first GoLang in Linux\n")
}

Below commands will do both compile and run the code:

go install $GOPATH/src/hello/hello.go
$GOBIN/hello
Output:
Hello from dotsway.com, you have created your first GoLang in Linux

Thanks!

Install GOLang on Linux

GOLang Programming Language
What is GOLang?

It's a programming language which started at 2007 by Google, the main initiative was to have a language based on C++ but with modern syntax.

What is GOLang Good For ?

I have seen people doing some servers' background jobs with it, also a lot of popular open source apps were created by GoLang like Docker for example, still i don't see it as all around language like Java for example.

Popular Open Source Applications Done by GOLang:

Docker, Kubernetes,Etcs, Desi, Flynn, Lime, Revel, InfluxDB and load more of other open sources are built by golang

How to install go on Linux

Go to any of your desired folder to download golang package and replace the url after wget by the latest version you find here. "https://golang.org/dl/"

In below example i am using --no-check-certificate to ignore any invalid SSL certificate since i am sure of the source.

cd /home/dotsway/
wget --no-check-certificate https://storage.googleapis.com/golang/go1.8.1.linux-amd64.tar.gz

Extract the tarball:

tar xvfz go1.8.1.linux-amd64.tar.gz
cp -R /home/dotsway/go /usr/local/

Now you need add 'go' to your Linux environment, click here to know how to add path to Linux environment.