Golang — Devil is in the Details

Ranjeet Kaur
4 min readJul 7, 2018

In this article, we will discuss about how to code in Go. If you are new to Go, and want to understand the basic modules of Go code, you can read the previous article.

Structs: For all of you, who come from Java background, you know the pain of writing constructors to cover range of input formats. Go gives you a simple solution for defining objects in a single line without going to the pain of writing tens of constructors. Following are few examples of how you can define objects in Go.

type Person struct{
Id int
Name string
Age int
Email string
}
func foo(){
//Object with No arguments
person1 := Person{}

//Object with all arguments
person2 := Person{Id:1,Name:"JaneDoe",Age:24,Email:"x@abc.com"}
//Object with partial arguments
person3 := Person{Name:"JohnDoe"}
}

Arrays vs Slices: Arrays in Go are fixed length collection of objects of same type, whereas slices are variable length collections. In general, slices are much more powerful than Arrays and are used much more extensively.

Arrays in Go are fixed length collection of objects of same type. In Go, Arrays are just values, hence when you assign an array to a variable, or pass the value to a function, a copy of the array is created and passed. Arrays can be declared by giving size in the declaration

var arr1 = [5]int{}

Or by letting the compiler calculate the length.

var arr2 = [...]int{1,2,3}

Length of an array can be accessed by using inbuilt function len.

length := len(arr1)

Slices in Go, are variable length collections of same data type. Go Slice is a powerful data structure, built over Arrays, keeping ease of use and scalability in mind. Unlike arrays, slices are not of fixed length. While declaring a Slice, you don’t need to provide size.

var sl1 = []int{}
var sl2 = []int{1,2,3}

You can also make a slice with length and capacity options, with an inbuilt function make.

//var sl3 = make([]int, length, capacity)
var sl3 = make([]int, 5, 5)

If the capacity is omitted, then it defaults to length value

//var sl4 = make([]int, length)
var sl4 = make([]int, 5)

And to access length and capacity of a slice, inbuilt functions len() and cap() functions can be used.

length := len(sl1)
capacity := cap(sl1)

To dynamically add values in slice, we can simply append the new value to the existing slice.

sl2 = append(sl2, 4)

To traverse over an array or slice , a simple for loop can be used with the range command. Here i represents index of the array and value represents the actual value in the array.

for i, val := range sl2 {
...
}

We can also ignore index or value by replacing it with an underscore.

for i, _ := range sl1 {
...
}

Effectively, the above statement is same as following.

for i := 0; i < len(sl2); i++ {
...
}

Maps: Maps in Go are structurally similar to slices and can be defined either through make function or by directly defining object of type map[keyType]valueType.

map1 := make(map[int]string)
map2 := map[int]string{}
map3 := map[int]string{1:"A", 2:"B"}

Values in a map can be set or accessed by providing key in square brackets[].

map1[key] = "Test"
value := map1[key]

To check whether a value exists in the map, if statement with a pre-condition can be used. Here ok is a boolean value signifying whether key exists or not.

if _, ok := map1[key];ok {
...
}

Go provides an inbuilt function, to delete values from the map.

delete(map1, key)

Length of a map can be retrieved in same way as a slice or an array.

n := len(map1)

To traverse over a map, a simple for loop can be used with the range command.

for key, value := range map1 {
...
}

Conditions: When declaring a condition block in Go, parenthesis() around the condition can be ignored but braces{} for the execution block is mandatory. Go has two types of conditions, If/Else and Switch.

If/Else in Go is similar to any other language, with one difference being that in addition to providing condition, a small init statement can also be added.

if i := a * b; i < c {
val += i
}

Else If and else can be given in same way as any other language.

if a < b {
return 1
} else if a > b {
return -1
} else {
return 0
}

Switch in Go is again similar to other languages.

switch a {
case 1: return "a"
default
: return ""
}

One extra feature that Go provides, in Switch is that you can add conditional switch.

switch {
case a>b:return 1
case b>a:return -1
default: 0
}

Loops: Go has only one type of loop, the for loop. Similar to conditions parenthesis() can be skipped in loops, but braces {} are mandatory. A basic for loop can be defined, with init statement, execution condition and post command.

for i := 0; i < 10; i++ {
val += i
}

Init statement and Post command are both optional and can be omitted. Semicolons can be removed to make it effectively same as a while loop.

for i < 10 {
val += i
i++
}

An infinite loop can be run by simply skipping the execution condition.

for {
val += i
}

Complete Code for this tutorial can be accessed through the GitHub Repo.

I hope you find it helpful. If you have any suggestions, please feel free to drop a comment.

In the Next Article, we will discuss how to integrate MySQL in Go.

--

--