Golang – Introduction4 min read

What is Golang?

Golang or Go is an open-source programming language aimed at being simple, dependable, and efficient.

In 2007, Google created the Go programming language. Google was developing significantly at the time, and the code used to manage its infrastructure was growing in both size and complexity at the same time. Go was created by Robert Griesemer, Rob Pike, and Ken Thompson.

Go is influenced by C, although it places a larger focus on simplicity and security. The language is comprised of the following statements:

  • A syntax and environment that follows the patterns found in dynamic languages:
    • Optional type inference for variable declaration and initialization (x := 0 instead of int x = 0; or var x = 0;).
    • Remote package management.
    • Fast Compilation.
  • Approaches to specific problems that are unique:
    • Lightweight processes (goroutines), channels, and the select statement are all built-in concurrency primitives.
    • In place of virtual inheritance, an interface system is used, and type embedding is used instead of non-virtual inheritance.
    • A toolchain that generates statically linked native binaries by default, with no external dependencies.
  • A goal to keep the language specification as basic as possible for programmers to remember.

Environment Setup

To get started with golang, the very first requirement is to set up an environment for it so that you can create and execute the golang programs. There are only 2 requirements for this setup,

  1. Go compiler
  2. Text Editor (eg, Atom, VS code, Sublime, etc)

1. Download the golang compiler from the below link as per your operating system

Download Golang

Once downloaded, install the compiler. You can verify the installation by executing the below command in cmd/terminal depending upon your operating system

go version

If the above command shows output like below, it means that the compiler has been installed successfully.

golang version

2. Text Editor

As mentioned earlier, you can download any Editor. For this tutorial, we will be working on the Atom editor. You can download Atom from the below link

Download Atom

Once downloaded, simply install the editor.


Go Program Structure

A golang program consists of the following features

  1. Package declaration – It is the first line of code in golang. It defines the package name in which the program should lie.
  2. Importing packages – here we can import other packages. Fmt is one if such packages. Fmt is a preprocessor command which tells the go compiler to include the files lying in the package fmt.
  3. Functions – here we can define our functions. Just like in java, here, the execution of the program starts from the main() function and it is mandatory.
  4. Statements – inside our functions, we can write statements and expressions.
  5. Comments – We can add a comment by adding 2 backslashes before the comment text.

Hello World in go

Let us now see our very first program in golang. We will create a program to print Hello World. If you have installed Atom, follow the below steps to create a go program in Atom.

  1. Create a folder in your system
  2. Start Atom and open the folder that you created in the step 1.
  3. Right-click on the folder -> New File -> name it Hello.go
  4. Paste the below code in the file
// Hello World program in Go
package main
import "fmt"
func main(){
  fmt.Println("Hello World!!!")
}

Click on File and save the file. Open the cmd and navigate to the location where your go file is and execute the below command to run the program

go run Hello.go

Output –

C:\Users\Hiberstack\GoPrograms>go run Hello.go
Hello World!!!
golang program

As we can see from the above program, we used fmt.Print() function to print the text. Just like this,  there are several other ways to print in go. Refer to the below program

package main
import "fmt"
func main(){
	fmt.Print("print 1") //the next statement will be printed on the same line
	fmt.Println(" print 2") //the next statement will be printed on the next line
	print("print 3") //same as fmt.Print()
	println(" print 4") //same as fmt.Println()
}

Output –

C:\Users\Hiberstack\GoPrograms>go run PrintDemo.go
print 1 print 2
print 3 print 4

Share:

Leave a Reply