Get started with gRPC

gRPC is a Remote Procedure Call (RPC) framework defined on grpc.io as: gRPC is a modern open source high performance Remote Procedure Call (RPC) framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authentication. It is also applicable in last mile of distributed computing to connect devices, mobile applications and browsers to backend services. [Read More]
grpc  go 

Go Variadic Function

Example of Go variadic function: func main() { nums := []int{1, 2, 3} // uses the existing slice s1 := sum(nums...) // creates a new slice s2 := sum(1, 2, 3) // Both s1 and s2 values are equal // Variadic func can be called with no argument s3 := sum() } func sum(nums ...int) (total int) { for _, n := range nums { total += n } } The common example is the Println function from the fmt package. [Read More]
go  note 

Pass slice to a function

Here is an example of slice passed as value to a function: func main() { dirs := []string{"up", "down", "left", "right"} up(dirs) // dirs => []string{"UP", "DOWN", "LEFT", "RIGHT"} } func up(list []string) { // slice pointer points to a reference // even if they passed in a function. // So it modify the original slice // elements to upper case. for i := range list{ list[i] = strings. [Read More]
go  note 

Struct in Go

Declare a struct: type person struct { name, lastname string age int } var alice struct { name, lastname string age int } Struct instance: picasso := person{ name: "Pablo", lastname: "Picasso", age: 91, } alice.name = "Alice" alice.lastname = "Wonderland" alice.age = 21 Note: Struct can be compared. You can’t compare struct values that contains incomparable fields. You need to compare them manually. Embed struct to another struct: type text struct { title string words int } type book struct { // embed the text text isbn string } Say b1 is instance of book stuct, then b1. [Read More]
go  note 

Input Scanning using bufio in Go

Read standard input by line: import ( "bufio" "os" "fmt" ) func main() { // New scanner to read from stdin. // By default it reads line. ie. split // function defaults to ScanLines. in := bufio.NewScanner(os.Stdin) for in.Scan() { // Read line and print it. fmt.Println(in.Text()) } } Read by words: // Set split function to ScanWords before Scan() function. in.Split(bufio.ScanWords) for in.Scan() { . [Read More]
go  note 

Read Write files in Go

Package ioutil implements I/O functions.

import "ioutil"

Read file:

config, error := ioutil.ReadFile("/etc/config")

Write to file:

error := ioutil.WriteFile("/etc/config", config, 0644)

Read direcrtory:

// ioutil.ReadDir returns slice of os.FileInfo
files, error := ioutil.ReadDir("/home/akshay")

if error != nil {
    // Handle error
}

for _, file := range files {
    // file.Size()
    // file.Name()
    // file.IsDir()
}

For more functions, read ioutil doc.

go  note 

Map in Go

Declare a map:

// Nil map: Read-Only
var di map[string]string


dict := map[string]string{
	"good":    "iyi",
	"great":   "harika",
	"perfect": "m√ľkemmel",
}

Delete an element of the map using delete builtin:

delete(dict, "awesome")

Retrieve an value from map:

if value, ok := dict["great"]; ok {
    fmt.Printf("%q found in dict", value)
}

Iterate through map elements:

for key, value := range dict {
    fmt.Println(key, value)
}
go  note 

Copy in Go

Copy slice to another slice: evens := []int{2, 4} odds := []int{3, 5, 7} N := copy(evens, odds) fmt.Printf("%d element(s) are copied.\n", N) // evens after copy is, [3, 5] // odds is intact after copy, [3, 5, 7] copy can be used over append if want to keep length of target slice intact. If the source slice is bigger in size than destination, copy only copies slice elements that occupies in target slice. [Read More]
go  note 

Full slice expression

Simple slice expression: “[low:high]” numbers := [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} s := numbers[1:4] fmt.Println(s) // [1, 2, 3] fmt.Println(len(s)) // len = 3 fmt.Println(cap(s)) // cap = 9 Full slice expression: “[low:high:max]” numbers := [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} s := numbers[1:4:5] fmt.Println(s) // [1, 2, 3] fmt.Println(len(s)) // len = 3 fmt.Println(cap(s)) // cap = 4 Full slice expression can be used for limit backing array sharing. [Read More]
go  note 

Go errors

  1. non-constant array bound max

Array length is not a constant.

  1. slice can only be compared to nil

Invalid comparison between slices. Slice can only be compared to nil.

go  note