Get started with Go lang

 

What is Go?

Go is also known as Go lang. It's an open source programming language developed by Google. It is a simple language which is statically typed. It is very popular for it's concurrency support via go routines & channels. Go Language has garbage collection which itself does the memory management and allows the deferred execution of functions.


How to get started? 

For Windows and MacOS : 

Please head to following link to download and install go lang on your personal device. 

Download Go

 

You'll see the interface like below for downloading.




After downloading the package, just install it and to be sure that Go has successfully installed on your system, Run the below command in your powershell or terminal : 

go version


If you see the version correctly , then we are good to go.


For Linux : 

Linux have Go lang in their repositories. But still I would recommend the following version manager for installing and maintaining Go versions in your system. 

"g" - Go version manager


After Installing Go in your system : 

After installing Go , we need an IDE for coding or writing our code.

I recommend VS Code as it is really popular and I also use it.


Download VS Code from here  : Visual studio code


After installing VS Code, we have to install the extension for Go lang in VS Code.

It's name is GO, shown in the screenshot below.

 



Update the Go tools : 

1. In Visual Studio Code, open Command Palette's Help > Show All Commands. Or use the keyboard shortcut (Ctrl+Shift+P)




2.  Search for Go: Install/Update tools then run the command from the pallet



3. When prompted, select all the available Go tools then click OK.



4. Wait for the Go tools to finish updating.

 


 


Let's write our first code in Go: 

Create a file named main.go , here .go is the extension for go files.




Then , type the below code in the main.go file : 

 

package main

import "fmt"

func main(){
fmt.Println("Hello world!")
}


Ok, let's discuss the code now. 


In the first line, we can see the line package main, it means our main.go is part of the main package. 


The import line imports packages in go lang. To print something to the screen , we need the "fmt" package, So, we have to import it beforehand.


In the next line we can see the line func main(){} it means the main function starts from here.

main function is the starting point of any program. In the main function we have write the logic of our code.


Here, we wrote fmt.Println(""), Here Println is a function of fmt package and it can print any line on the screen and also adds a new line at the end. We can write whatever we want to show on the screen between the "" signs inside the Println function.


We can run the program by typing the below command in the terminal : 

go run main.go


Our output will be something like this : 



You can also try printing whatever you want!


Congratulations!

You've now successfully created your first Go program. We will learn more about Go lang in the next blog.




Comments

Popular posts from this blog

More Go (Go lang learning series blog - 2)