Go - Maps

Intro Maps are somewhat similar to what other languages call “dictionaries” or “hashes”. A map maps keys to values. Here are some points to look at: Maps allows us to quickly access to an element/value using a unique key. Map keys must be unique because otherwise it can’t find the corresponding values/elements. The types of Map Keys and Values in Maps can be different. A Map Key must be a comparable type. [Read More]

Go loop - break and continue

Intro break will break out of a loop. It’s a way to stop looping. continue will move on to the next iteration. Let’s see it in action. Examples Example - break package main import "fmt" func main() { pow := make([]int, 10) for i := range pow { pow[i] = 1 << uint(i) if pow[i] >= 16 { break } } fmt.Println(pow) // [1 2 4 8 16 0 0 0 0 0] } Example - continue package main import "fmt" func main() { pow := make([]int, 10) for i := range pow { if i%2 == 0 { continue } pow[i] = 1 << uint(i) } fmt. [Read More]

Wait for goroutines Using WaitGroups

Syntax A sync.WaitGroup waits for a group of goroutines to finish. var wg sync.WaitGroup wg.Add(2) go func() { // Do work wg.Done() }() go func() { // Do work wg.Done() }() wg.Wait() First the main goroutine calls Add to set the number of goroutines to wait for. Then two new goroutines run and call Done when finished. At the same time, Wait is used to block until these two goroutines have finished. [Read More]

Why use gRPC?

With gRPC we can define our service once in a .proto file and implement clients and servers in any of gRPC’s supported languages, which in turn can be run in environments ranging from servers inside Google to your own tablet - all the complexity of communication between different languages and environments is handled for you by gRPC. We also get all the advantages of working with protocol buffers, including efficient serialization, a simple IDL, and easy interface updating. [Read More]

Adding behaviour to any type using Type Aliasing

To define methods on a type you don’t “own”, you need to define an alias for the type you want to extend:

package main

import (
    "fmt"
    "strings"
)

type MyStr string

func (s MyStr) Uppercase() string {
    return strings.ToUpper(string(s))
}

func main() {
    fmt.Println(MyStr("test").Uppercase())
}

http://www.golangbootcamp.com/book/methods

Attaching Method to any type

  • We can attach methods to any type in Go. For e.g.

    // Basic Types
    int
    string
    float64
    
    // Bare Types
    array
    struct
    
    // -----------------------
    // Do not use "Pointer Receivers" with below types since they already carry a pointer with themselves.
    // i.e. slice, map, chan, func
    // -----------------------
    // Pointer Bearing Types
    slice
    map
    chan // Channels
    
    // We can also attach methods to:
    func
    

Challenges with gRPC

Challenges with gRPC You should bear in mind that whilst gRPC does allow you to utilize these newer bits of technology, it is more challenging prototyping a gRPC service due to the fact that tools like the Postman HTTP client cannot be used in order to easily interact with your exposed gRPC service. You do have options that make this possible, but it’s not something that’s readily available natively. There are options to use tools such as envoy to reverse proxy standard JSON requests and transcode them into the right data format but this is an additional dependency that can be tricky to set up for simple projects. [Read More]

Code Organization

Methods can be defined on any file in the package, but my recommendation is to organize the code as shown below: Define constants Define vars Define interface Define struct Implementation of methods on struct Example: package models // list of packages to import import ( "fmt" ) // list of constants const ( ConstExample = "const before vars" ) // list of variables var ( ExportedVar = 42 nonExportedVar = "so say we all" ) type Fly interface { Fly() string } // Main type(s) for the file, // try to keep the lowest amount of structs per file when possible. [Read More]

Go - for loop

Go has only one looping construct, the for loop. The basic for loop looks as it does in C or Java, except that the ( ) are gone (they are not even optional) and the { } are required. As in C or Java, you can leave the pre and post statements empty. for init; condition; post { } Examples Example - Go loop for j := 7; j <= 9; j++ { fmt. [Read More]
loop  for 

Go - select

Syntax The select statement waits for multiple send or receive operations simultaneously. // Blocks until there's data available on ch1 or ch2 select { case <-ch1: fmt.Println("Received from ch1") case <-ch2: fmt.Println("Received from ch2") } The statement blocks as a whole until one of the operations becomes unblocked. If several cases can proceed, a single one of them will be chosen at random. Send and receive operations on a nil channel block forever. [Read More]