Golang Slices

Intro A slice in Golang is an abstraction that sits on top of an array. An array normally has a set size and must be initialised with a size. A slice is a little more like an array in javascript or an ArrayList in Java. A slice is a descriptor of an array segment. It consists of a pointer to the array, the length of the segment, and its capacity (the maximum length of the segment). [Read More]

Implement Sort Interface

sort package defines an interface whose name is Interface: type Interface interface { // Len is the number of elements in the collection. Len() int // Less reports whether the element with // index i should sort before the element with index j. Less(i, j int) bool // Swap swaps the elements with indexes i and j. Swap(i, j int) } For slice, or any other collection types, provided that it implements the Len(), Less and Swap functions, you can use sort. [Read More]

Inheritance and Composition in GO

Structs can “inherit” from other structs by a method known as embeding. Here is a simple example: package main import ( "fmt" ) type a struct { Name string } //embeds a value of type a type b struct { a } //embeds a pointer to an a type c struct { *a } func main() { a := a{Name: "Janeway"} fmt.Println(a.Name) b := &b{a: a} fmt.Println(b.Name) c := &c{a: &a} fmt. [Read More]

Method receiver vs Pointer receiver

Struct values when used as method receivers are exactly that values and so are a shallow copy of the value allocated a new portion of memory. The effects are not observed outside of the method as there are no references to the new value and so it is garbaged collected. Pointer receivers allow mutation of what the pointer points to. Your function is recieving a pointer to the same address in memory even in the function stackframe. [Read More]

Methods in GO

Intro We have already seen golang functions. Now the question is: what is the difference between a function and a method in Go? Answer is - A method is a function that has a defined receiver, in OOP terms, a method is a function on an instance of an object. Go doesnt have classes but structs, and we can add receiver methods on structs to add behaviour. To add some behaviour to a struct you can have it be a method reciever. [Read More]

Understanding the GOPATH environment variable

The GOPATH environment variable lists places to look for Go code. It defines your workspace. It is likely the only environment variable you’ll need to set when developing Go code. Official documentation: How to Write Go Code: The GOPATH environment variable Command go: GOPATH environment variable Normally the GOPATH is set in your shell profile (one of ~/.bashrc, ~/.bash_profile, etc). When you install packages and build binaries, the Go tool will look for source code in $GOPATH/src/ followed by a package import path in order to resolve dependencies. [Read More]
get 

Channels in Go

Intro A go maxim or proverb is: Do not communicate by sharing memory; instead, share memory by communicating. So what is a channel? A channel is a “typed” conduit (pipes) mechanism for goroutines to synchronize execution and communicate by passing values, using channel operator, <-. They are mechanism for communication between goroutines. Syntax // can only be used to send float64s chan<- float64 // can only be used to receive ints <-chan int // can be used to send and receive values of type Dosa chan Dosa (The data flows in the direction of the arrow. [Read More]

Go - Array

Syntax In Go, the length is also a part of array type. So the following code declares an array: var array [3]int while “var slice []int” defines a slice. Arrays cannot be resized. Another way is to set the array entries as you declare the array: func main() { a := [2]string{"hello", "world!"} fmt.Printf("%q", a) } You can also use an implicit length when passing values to array using [...]: [Read More]

Go Conditionals - switch

Intro In addition to the switch keyword, a switch statement has cases. The switch statement switches on some case. Example - No break required Compared to other programming languages (such as C), Go’s switch-case statement doesn’t need explicit “break”, and not have fall-though characteristic. Take the following code as an example: package main import ( "fmt" ) func checkSwitch(val int) { switch val { case 0: case 1: fmt.Println("The value is: ", val) } } func main() { checkSwitch(0) checkSwitch(1) } The output is: [Read More]

Go Configuration - Environment variables

Open up .profile or .zshrc or .bashrc depending on our OS and add/edit following: #!/bin/bash # Specifies where the Go destribution is installed on the system. export GOROOT=/usr/local/go # Specifies top-level directory containing source code for all our Go projects. # Inside this directory, we need to create 3 more directories viz. "src", "pkg" and "bin". export GOPATH=~/adiwork/go # This directory is also known as Go Workspace. # "src" directory inside Workspace represents where all the Go source code will be stored. [Read More]
get