Go - String

In Go, string is an immutable array of bytes. So if created, we can’t change its value. E.g.: package main func main() { s := "Hello" s[0] = 'h' } The compiler will complain: cannot assign to s[0] To modify the content of a string, you could convert it to a byte array. But in fact, you do not operate on the original string, just a copy: package main import "fmt" func main() { s := "Hello" b := []byte(s) b[0] = 'h' fmt. [Read More]

Go - Structs

Intro A struct is a collection of fields/properties. You can define new types as structs or interfaces. If you are coming from an object-oriented background, you can think of a struct to be a light class that supports composition but not inheritance. Structs are blueprints — They are fixed and created at compile-time, but struct values fill them in runtime.. It’s like a class in OOP languages. Groups related data in a single type. [Read More]

Go - Type conversion

The expression T(v) converts the value v to the type T. Some numeric conversions: var i int = 42 var f float64 = float64(i) var u uint = uint(f) Or, put more simply: i := 42 f := float64(i) u := uint(f) Go assignment between items of different type requires an explicit conversion which means that you manually need to convert types if you are passing a variable to a function expecting another type. [Read More]

Go Conditinals - if else statement

if Statement Simple if syntax The if statement looks as it does in C or Java, except that the ( ) are gone and the { } are required. Like for, the if statement can start with a short statement to execute before the condition. Variables declared by the statement are only in scope until the end of the if. Variables declared inside an if short statement are also available inside any of the else blocks. [Read More]

Go functions

A function can take zero or more typed arguments. The type comes after the variable name. Functions can be defined to return any number of values that are always typed. package main import "fmt" func add(x int, y int) int { return x + y } func main() { fmt.Println(add(42, 13)) } Go Tour Functions example In the following example, instead of declaring the type of each parameter, we only declare one type that applies to both. [Read More]

Go Packages and Import

All package files, should be in the same (single) directory. i.e. all package source code files should be located in a one single directory. All files in a specific folder should belong to a one single package. It’s a convention, not a rule. package clause can be used only once per file and it should be the first line in .go source file. Package contains multiple Go files belonging to same folder. [Read More]

Go Pointers

Go has pointers, but no pointer arithmetic. Struct fields can be accessed through a struct pointer. The indirection through the pointer is transparent (you can directly call fields and methods on a pointer). Note that by default Go passes arguments by value (copying the arguments), if you want to pass the arguments by reference, you need to pass pointers (or use a structure using reference values like slices (Section 4.2) and maps (Section 4. [Read More]

Goroutines - Go Coroutines

Syntax A goroutine is a lightweight thread managed by the Go runtime. go f(x, y, z) // notice go keyword starts a new goroutine running: f(x, y, z) The evaluation of f, x, y, and z happens in the current goroutine and the execution of f happens in the new goroutine. All goroutines in a single program share the same address space. So access to shared memory must be synchronized. The sync package provides useful primitives, although you won’t need them much in Go as there are other primitives. [Read More]

gRPC Introduction

So, before we dive in, we first need to understand what gRPC is, how it works and so on. Definition - gRPC is a modern, open source remote procedure call (RPC) framework that can run anywhere Remote Procedure Calls are something that we use within distributed systems that allow us to communicate between applications. More specifically, it allows us to expose methods within our application that we want other applications to be able to invoke. [Read More]