Basics Of Golang - Part One

Introduction

 
The Go Programming language, also commonly known as Golang, is an open-source language which can make programmers' lives easy, esp those who develop performance intensive applications along with many other features.
 
The syntax of Golang is different than other object-oriented languages but in a way it's a lot like C Prog Language.
 

Short Agenda 

 
All developers who are interested to quickly start learning Golang can use this article as a reference point.
 
Agenda
  • Pre-requisites
  • How to run Go Program?
  • Go Command Line Interfaces
  • Go Packages
Pre-Requisites
 
Golang can be used with any IDE’s like Visual Studio Code, Sublime, Notepad++ etc but Golang needs to be downloaded and installed from this link : golang.com/dl based on the OS configuration.
 
The preferred choice of IDE by most developers is VS Code.
 
Once Golang is downloaded and installed, users can install Golang extensions available in VS Code. This extension will help in early detection of syntactical errors, formatting the code, and automatically import the packages based on the functions used in the code.
 

How to Write & Run a Go Program?

 
Lets take our very own favorite “Hello World!” Program,
 
Note
Every Golang program has extension of “.go”, meaning the  file name will be “main.go” and like any other application Golang’s startup file is main.go and within that a main function.
 

If we observe the above program carefully, the first line indicates that the below program is part of package main, and then we have the main function which the compiler would look for for its execution. Now, fmt.Println(“Hello World!”) is responsible for displaying the “Hello World!” message on the console but in order to use “Println” function we have to import “fmt” package hence there is import “fmt” statement.
 
Now, in order to run the above program generally there are 2 ways,
 
Navigate to the program’s directory,
 
Go to build main.go. This command would compile the program and create an executable file in the same directory and then the user can directly run the executable file.
 
 
Go run main.go. This command would do both the things; compile and run, but it wont create an executable file.
 
 

Final Verdict

 
Generally developers use go run main.go, which saves an additional step of running an executable file, but it can vary based on preference.
 
Go Command Line Interfaces
 
There are 6 commands of Golang which are most widely used,
  1. go build : compiles source code files and create an executable file.
  2. go run : compiles and executes the files.
  3. go fmt : formats all the code in each file in the current directory.
  4. go install : compiles and installs the package specified in command.
  5. go get : downloads the raw source code of package in command.
  6. go test : runs any test associated with current project.
Go Packages
 
Packages in Go is nothing but Project or Namespace in other languages. It means that there can be more than 1 file as part of the same package.
 
There are 2 types of packages in Go,
  1. Executable Package
    Defines a package that can be compiled and executed, must have function “func main()”.

  2. Reusable Package
    Defines a package that can be used as a dependency in other packages. For ex : “fmt” is a reusable package which is used in our “Hello World!” program
Go Packages
 
Packages in Go is nothing but Project or Namespace in other languages. It means that there can be more than 1 file as part of the same package.
 
There are 2 types of packages in Go,
  1. Executable Package
    Defines a package that can be compiled and executed, must have function “func main()”.

  2. Reusable Package
    Defines a package that can be used as a dependency in other packages. For ex : “fmt” is a reusable package which is used in our “Hello World!” program
Variable Declarations
 
There are 3 ways to declare and assign variables in Golang,
 
The first way and second way are quite common, but the third way is a specialty of Golang wherein, if you use (:=) operator it indicates that on left hand side you have a new variable andon the  right hand side is the value that you are trying to assign.