Golang – Best Parts – Part 1

Hello,

In this series I will try to go through with golang  programming language tutorials and best parts of the language.

Most of these examples and comments are taken from official documentation of golang. Please visit the website for full experience.

I will write parts as I learn and try to reference video tutorials and best practices as well.

Lets get started;

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.

 

If you are upgrading from an older version of Go you must first remove the existing version.

For example, if you installed Go to your home directory you should add the following commands to $HOME/.profile:

 

hello world

example below can be named different than “hello.go”. Just rename file accordingly and build should run.

gobin and goroot environment variables

 

go tool tour

%g replacement

In Go, a name is exported if it begins with a capital letter.

functions

Multiple results

A function can return any number of results.

Naked/Named return statements should be used only in short functions, as with the example shown here. They can harm readability in longer functions.

variables

Short variable declarations

Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type

Type inference

Constants cannot be declared using the := syntax.

shifting

Go has only one looping construct, the for loop.

Forever

If you omit the loop condition it loops forever, so an infinite loop is compactly expressed.

If

Go’s if statements are like its for loops; the expression need not be surrounded by parentheses ( ) but the braces { } are required.

Switch

You probably knew what switch was going to look like.

A case body breaks automatically, unless it ends with a fallthrough statement.

Defer

A defer statement defers the execution of a function until the surrounding function returns.

The deferred call’s arguments are evaluated immediately, but the function call is not executed until the surrounding function returns.

Deferred function calls are pushed onto a stack. When a function returns, its deferred calls are executed in last-in-first-out order.