GO – Getting Started

GO or Golang is known for its Efficiency, Concurrency and Scalability to large systems. Some of its distinctive features are:

  • Clean, readable and modular code with minimum boilerplate.
  • Is Statically typed
  • Faster compilation time
  • Remote Package Management
  • goroutines (light-weight processes), channels (connects goroutins) and the Select statement
  • Supports Networking and Multiprocessing.

GO was created at google in 2009 by Robert Griesemer, Rob Pike and Ken Thompson.

Gopher-ru

Syntax:

Let us start by understanding the basic structure and syntax.

package main

import (
  "fmt"
  "time"
)

func main() {
  fmt.Println("Welcome to Golang! The time is: ", time.Now())
}
  • “main” is the package where the execution starts
  • Import: The above code uses the packages “fmt” and “time”. Multiple packages can be grouped into a single import statement (by enclosing within paranthesis).
  • Exported names from a package should always start with a capital letter (Ex. time.Now() – ‘Now’  is the exported name from the package ‘time’)

Compile and run on local:

  • Create a file welcome.go in the project directory. Ensure the PATH variable includes the go installation directory (“/usr/local/go/bin”)
  • go build (creates an executable)
  • ./golang – to execute

Functions:

Function can take zero or more arguments.

func add(x int, y int) int {
	return x + y
}

Notice that the type is written after the variable name. The type of the returned result is specified after the paranthesis.

  • When two or more function parameters share the same type(x int, y int) , it can be written as (x,y int)
  • When the function returns more than one result, it is written as:
func sum(x,y int) (string,int) {
   return "sum: ", x+y
}
  • Naked return statement: Returns the named return values. Should be used in short functions only.
func sampleret(x,y int) (a,b int) {
   a = x + 10/3
   b = y + 10/3
   return
}

Variables:

‘var’ statement declares a list of variables. They can be at the Package level or Function level.

Different ways of declaring variables:

var i int
var x,y int = 10, 11
a := 3 

a := 3 is a short assignment statement that can be used in the place of var inside the function. It is not available outside the function.

Basic types:

  • List of basic types:
bool
string

int  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64 uintptr

byte 

rune (represents a Unicode code point)

float32 float64

complex64 complex128
  • Representing Zero value for various types:
0 -> for int
"" (empty string) -> for strings
false -> for boolean
  • Type Conversion:
var i int = 100
var j float64 = float64(i)
  • Type Inference:

When a variable is declared without explicitly specifying its type, it either takes the same type as the value on the right hand side:

var i int = 20
j := i (j is int)

or assigns a type depending on the value: i := 1.1234 (float64)

  • Constants:

const Pi = 3.14 – Const cannot be declared using ‘:=’

In this post, we discussed some of Go’s distinctive features and had a quick look at Structure, basic syntax, types and variables.

Next post: GO – Flow Control

Advertisement

One thought on “GO – Getting Started

  1. Pingback: GO – Flow Control – Reverie

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s