Whilst REST and gRPC are somewhat similar, there are some fundamental differences in how they work that you should be aware of.
gRPC utilizes HTTP/2 whereas REST utilizes HTTP 1.1 gRPC utilizes the protocol buffer data format as opposed to the standard JSON data format that is typically used within REST APIs. This provides us with strong schema and backward compatibility. With gRPC you can utilize HTTP/2 capabilities such as server-side streaming, client-side streaming or even bidirectional-streaming should you wish.
[Read More]
Interface in Go
Intro We declare an Interface much like as we define a user defined type. Interfaces decouple different types from each other so we can create more maintainable programs.
More:
An Interface is a Protocol, a Contract.
Bigger the Interface the weaker the abstraction. –> Rob Pike
It’s an abstract type. It doesn’t have any implementation. It only describes the expected behavior.
The opposite of Abstract Type is Concrete Type.
All the types in Go except Interface are of Concrete Type.
[Read More]
"go build" and "go install"
Let’s tidy up the $GOPATH directory and only keep Go source code files left over:
# tree . ├── bin ├── pkg └── src ├── greet │ └── greet.go └── hello └── hello.go 5 directories, 2 files The greet.go is greet package which just provides one function:
# cat src/greet/greet.go package greet import "fmt" func Greet() { fmt.Println("नमस्ते किंशुक!") } While hello.go is a main package which takes advantage of greet and can be built into an executable binary:
[Read More]
"go get" command
“go get” command is the standard way of downloading and installing packages and related dependencies.
The snippet above basically tells the compiler to import the crypto package available at the github.com/mattetti/goRailsYourself/crypto path. It doesn’t mean that the compiler will automatically pull down the repository, so where does it find the code?
You need to pull down the code yourself. The easiest way is to use the go get command provided by Go.
[Read More]
"nil slice" vs "nil map"
Slice and map are all reference types in Go, and their default values are nil:
package main import "fmt" func main() { var ( s []int m map[int]bool ) if s == nil { fmt.Println("The value of s is nil") } if m == nil { fmt.Println("The value of m is nil") } } The result is like this:
The value of s is nil The value of m is nil When a slice’s value is nil, you could also do operations on it, such as append:
[Read More]
Accessing Map
Map is a reference type which points to a hash table, and you can use it to construct a “key-value” database which is very handy in practice programming. E.g., the following code will calculate the count of every element in a slice:
package main import ( "fmt" ) func main() { s := []int{1, 1, 2, 2, 3, 3, 3} m := make(map[int]int) for _, v := range s { m[v]++ } for key, value := range m { fmt.
[Read More]
Adding multiple types to collection
I would generally recommend avoiding this, but sometimes it could be neccessary. To add multiple types to a single map you must declare the map to take the interface type. The interface type is comparable to Object in Java. Everyhing is an interface.
package main
import "fmt"
func main(){
m := map[string]interface{}{
"test":1,
"test2":"test",
}
fmt.Println(m)
s := []interface{}{
"test",
2,
}
fmt.Println(s)
}
Adding sugar - map, reduce and filter, etc to slice
Golang prides itself on being a simple , pragmatic language and tried to avoid sugar that its creators feel are unnecessary. So lets add some map reduce functions ourselves to see what is involved:
package main import "fmt" type MyList []string // a custom type can be a builtin type if you want it to be func (ml MyList) Filter(f func(string) bool) []string { na := []string{} for _, v := range ml { if add := f(v); add { na = append(na, v) } } return na } func (ml MyList) Reduce(f func(prev, current string, index int) string) []string { na := []string{} for i, v := range ml { if i == 0 { na = append(na, f("", v, i)) } else { na = append(na, f(ml[i-1], v, i)) } } return na } func (ml MyList) Map(f func(val string) *string) []string { na := []string{} for _, v := range ml { mVal := f(v) if nil !
[Read More]
Buffered read
bufio package provides buffered read functions. Let’s see an example:
(1) Create a test.txt file first:
cat test.txt Output:
abcd efg hijk lmn You can see test.txt contains 4 lines.
(2) See the following program:
package main import ( "bufio" "fmt" "io" "log" "os" ) func main() { f, err := os.Open("test.txt") if err != nil { log.Fatal(err) } r := bufio.NewReader(f) for { if s, err := r.ReadSlice('\n'); err == nil || err == io.
[Read More]
Common Abbreviations Used In Go
Following are some of the common Abbreviations used in Go standard libraries:
var s string // string var i int // index var num int // number var msg string // message var v string // value var val string // value var fv string // flag value var err error // error value var args []string // arguments var seen bool // has seen? var parsed bool // parsing ok?
[Read More]