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
[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, 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
Hello from, you have created your first GoLang in Linux


Subscribe to
for video tutorials updates