Writing first Go program

Introduction

Nowadays there is a new buzz word in the IT world i.e Golang.
The need for a highly efficient yet simple programming language has lead to the creation of this new language name Go.

As per official documentation, Go is
The Go programming language is an open source project to make programmers more productive. Go is expressive, concise, clean, and efficient. Its concurrency mechanisms make it easy to write programs that get the most out of multicore and networked machines, while its novel type system enables flexible and modular program construction. Go compiles quickly to machine code yet has the convenience of garbage collection and the power of run-time reflection. It's a fast, statically typed, compiled language that feels like a dynamically typed, interpreted language.
Being an IT professional and in my struggle for constantly updating myself with new tools I started learning Go.
I will be sharing my progress by writing blogs on my learnings.

Let's discuss writing our first Go program

Installing Go

Go is available for installation on all major operating systems like MAC, Linux, Windows.
For installing go follow the instruction here according to your operating system.
After the successful installation go version  will yield the following output

First Go Program

package main

import "fmt"

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


This is a Hello World program for Go programming language. Let's understand this line by line.

package main

Everything in GO is wrapped in packages. The command "package <package name>" indicated that code inside this source file is part of the defined package.
The package “main” tells the Go compiler that the package should compile as an executable program instead of a shared library. The main function in the package “main” will be the entry point of our executable program.

import "fmt"

This is an import statement which is used to import the predefined or user-defined packages.
FMT  is the package that has all the formatting I/O functions.

Function Definition

GO function starts with the keyword func followed by the name of the function. Syntax of writing a function in GO is 

func <function name>(<argument list>) <return type>{
}

The command inside the main function
fmt.Println("Hello, World!")
prints Hello, World! on the terminal.

How to run this code:
go run first.go

To know more about other go commands you can have a look at the documentation here.

I hope this helps you in your path of learning GO.
Stay tuned for some more articles on GO.



Comments

Popular posts from this blog

Spidering the web with Python

Word Vectorization

Machine Learning -Solution or Problem