Variables
All variables in Go are strongly typed meaning you have to declare the data type, for example:
The shorthand version of the above is to let Go infer the type, and use the :=
syntax to declare and set the value of a variable:
Type Conversion
Be clear over being cleaver!
In Go you need to explicitly convert types, for example:
Some examples:
main.go
package main
import "fmt"
func main() {
var a string = "foo"
fmt.Println(a)
var b int = 42
fmt.Println(b)
var c = true
fmt.Println(c)
d := 3.1415
fmt.Println(d)
var e int = int(d)
fmt.Println(e)
}
Arithmetic
Multi variables can be delard on one line:
a, b := 5, 6
c := a + b // addition
c := a - b // subtraction
c := a * b // multiplication
c := a / b // division
c := a % 3 // modulus
Comparisons
a, b := 5, 6
c := a == b // false
c := a != b // true
c := a < b // false
c := a <= b // false
c := a > b // true
c := a >= b // true
Examples: