Learning Go, Chapter 02
A floating point number cannot represent a decimal value exactly. Do not use them to represent money or any other value that must have an exact decimal representation.
A **rune** is a data type used to represent a Unicode code point, which is a unique integer value that corresponds to a character in the Unicode standard.
a rune is represented using the rune data type, which is an alias for the int32 type. This data type allows you to store a Unicode code point, which is a 32-bit integer, making it suitable for handling characters from all languages.
var first rune = 'J' // good
var last int32 = 'B' // bad - correct but confusingconst pi = 3.14 // Untyped constant
const pi float64 = 3.14 // Typed constantPerhaps surprisingly, the Go compiler allows you to create unread constants with const. This is because constants in Go are calculated at compile time and cannot have any side effects. This makes them easy to eliminate: if a constant isn’t used, it is simply not included in the compiled binary.
go - What is a rune? - Stack Overflow
package main
import "fmt"
func main() {
str := "hello"
runes := []rune(str) // Convert string to rune slice
runes[0] = 'H' // Modify the first character
newStr := string(runes) // Convert back to string
fmt.Println(newStr) // Output: Hello
}Tip
Using
:=has one limitation. If you are declaring a variable at the package level, you must usevarbecause:=is not legal outside of functions.
In some situations within functions, you should avoid :=:
- When initializing a variable to its zero value, use var x int. This makes it clear that the zero value is intended.
- When assigning an untyped constant or a literal to a variable and the default type for the constant or literal isn’t the type you want for the variable, use the long var form with the type specified. While it is legal to use a type conversion to specify the type of the value and use := to write
x := byte(20), it is idiomatic to writevar x byte = 20. - Because := allows you to assign to both new and existing variables, it sometimes creates new variables when you think you are reusing existing ones.

Constant Naming
In many languages, constants are always written in all uppercase letters, with words separated by underscores (names like INDEX_COUNTER or NUMBER_TRIES). Go does not follow this pattern. This is because Go uses the case of the first letter in the name of a package-level declaration to determine if the item is accessible outside the package.
Usage of Generics
How to write a library function in go that should work with any integer type(int,int64,uint) ?
Transitionally we use this approach ?
func SumInts(xs []int) int
func SumInt64s(xs []int64) int64
func SumUint8s(xs []uint8) uint8Using Generics
import "golang.org/x/exp/constraints"
func Sum[T constraints.Integer](nums []T) T {
var total T
for _, n := range nums {
total += n
}
return total
}We can create custom constraint like this,
type Integer interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uintptr
}In Go generics, the ~ operator means:
“Any type whose underlying type is X.”
| Pattern | Accepts int | Accepts type MyInt int |
|---|---|---|
int | ✅ Yes | ❌ No |
~int | ✅ Yes | ✅ Yes |

Const
styleguide | Style guides for Google-originated open-source projects