vishal rana
2 min readJul 2, 2019

Getting started with GO(GOLANG)

In this story, I’m going to walk you through what is Golang, how its different from other programming languages and how to get started with Golang. So fasten the seat belts we are about to get started.

Golang is a compiled programming language and it is statically typed language. Golang was designed at Google by three engineers: Robert Griesemer, Rob Pike, and Ken Thompson. Go has a structure similar to c. It is easy to understand.

Go is designed with simplicity in mind. Go has a very powerful build in the concept of concurrency. The compile time in go is very less as compared to other compiled languages.

Let’s set up go environment and get things started.

First, you need to download go, you can download it from here. You need to download a stable version suitable for your operating system. Follow the instruction defined inside every download section.

Once the download is completed, go to the downloaded directory and run the command.

tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz

This will extract the tar to /usr/local directory. To make go accessible throughout the system you need to add the path to the $PATH;

PATH=$PATH:/usr/local/go/bin

Now open your terminal and type:

go

You’ll be able to see all the commands available with go.

Now let's create a directory and start the coding part.

mkdir go-practice
cd go-practice

Now you are in the directory in which we are going to write go code. Now open your terminal and create a new file.

touch main.go

main.go will be our main file and the executable one also.

let’s write a simple function to Add two numbers.

package main;//with package main we have to include a main function otherwise it //will give error like:function main is undeclared in the main packagefunc main(){}

This main function will execute as soon as you run the build. Let’s create another function that will accept two integers and return the sum of those two.

package main;import("fmt")func sum(a int, b int) int{
return a+b;
}

func main(){
fmt.Println(sum(7,8));
}

In the above piece of code what we are doing is we are calling a function called SUM from the main function that will return the sum of two integers that is been passed as an argument and we are using the “fmt” package to print our result.

There is one more thing you need to take care of “GOPATH”;

The GOPATH environment variable is used to specify directories that contain the source for Go projects and their binaries.

No responses yet