Go Essential Training
Basics
Run a go file
go run welcome.go
Build a file
Build a distributable executable, ej, in windows, a welcome.exe
file will be created
go build welcome.go
Help
Run go help
to see el go commands
Numbers and assignments
var x int
to declare a var type int
, in this case the default value will be 0
Something to have in mind: In go
the types comes after
the variable name
for numbers, you have the types int, int8, int16, int32, int64, float32, float64
Assign Value: x = 1
Print Value: This is an example for printing the value and the type with this template fmt.PrintF("x=%v, type of %T\n", x, x) //x=1, type of int
New variable assign with two dots
x := 1
is the same as var x int = 1
, in the last case, the type int
could be omitted.
multiple variables in one line:
x, y, z := 1.0, 2.0, 3.0
in this case the type of the variables will be float64
Conditionals
if/else
in golang we don't need ()
to wrap the if/else statements
Examples:
x := 10
// Simple one
if x > 5 {
// do something
}
// And
if x > 5 && x < 11 {
// do something
}
// or
if x < 20 || x > 30 {
// do something
}
// if / else if / else
if x < 5{
// do something
} else if x == 3 {
// do something else
} else {
// else
}
with go with can have an optional initialization statement before the conditional,
in the below example, the variable frac and it's value is going to be set from the a / b
division before the frac > 0.5
conditional
a := 11.0
b := 20.0
if frac := a / b; frac > 0.5 {
fmt.Printf("a is more than half of b")
}
switch
Examples of switch, they don't have to be only numbers, the variable could have a string value too and make the conditional against it.
x := 2
// switch with the x as global
switch x {
case 1:
fmt.Printf("One")
case 2:
fmt.Printf("two")
case 3:
fmt.Printf("three")
default:
fmt.Printf("many")
}
// x being used in each case statement
switch {
case x > 100:
fmt.Printf("x is very big")
case x > 10:
fmt.Printf("x is big")
default:
fmt.Printf("x is small")
}
Iteration
In golang, we only have one iteration loop, the for loop
:
Common loop
for i := 0; i < 3; i++ {
fmt.Println(i) // 0, 1, 2
}
Break
if you use break
, you exit the loop early.
for i := 0; i < 3; i++ {
if i > 1 {
break
}
fmt.Println(i) // 0, 1
}
Continue
to move to the next iteration without executing the code below it, you can use continue
for i := 0; i < 3; i++ {
if i < 1 {
continue
}
fmt.Println(i) // 1, 2
}
like a while
loop
go doesn't have a while interation, but this for example it's very much like it:
a := 0
for a < 3 {
fmt.Println(a) // 0, 1, 2
a++
}
like a while true
loop
A for loop without any condition on it, but you need to increment the variable value and set the break
for exiting the loop
b := 0
for {
if b > 3 {
break
}
fmt.Println(b) // 0, 1, 2, 3
b++
}
Strings
Strings in go:
book := "My Book title"
// Length of string
fmt.Println(len(book)) // 13
// You can access individual bytes with square brackets
fmt.Printf("book[0] = %v (type %T)\n", book[0], book[0])
// book[0] = 77 (type uint8) -> Uint8 in go is a byte
Note: Even though you can get the value of the character with book[0]
you can't set the value to another character like book[0] = 116
, it will give an error.
Slicing
You can access part of the string in go with slice
book := "My Book title"
// Slice (start, end), 0 base, half empty range, meaning:
// it will print character in position 4
// but it will not print character in position 11
fmt.Println(book[4:11]) // ook tit
// Slice (no end)
fmt.Println(book[4:]) // ook title
// Slice (no start)
fmt.Println(book[:4]) // My B
Concatenate, unicode & Multi line
book := "My Book title"
// Use `+` to concatenate strings
fmt.Println("t" + book[1:]) // ty Book title
// Unicode
fmt.Println("It was ½ price!") // It was ½ price!
// Multi line
multipleLines := `
First line
Second line
...
`
fmt.Println(multipleLines)
/*
First line
Second line
...
*/
Converting int
to string
n := 42
// SprintF will create a new string from int `n`
s := fmt.Sprintf("%d", n)
fmt.Printf("s = %q (type %T)\n", s, s) // s = "42" (type string)