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 - 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]