Golang has a lot of data types. If you programmed in other languages before, you may be familiar with int, float or byte.
Data types can be grouped into 3 types: bool, numeric types and string
Of numeric types there are a lot!
- int8, int16, int32, int64, int
- uint8, uint16, uint32, uint64, uint
- float32, float64
- complex64, complex128
- byte
- rune
I'll explain some below.
Integers
In Golang, things work a bit differently. You can't just define an integer int, you have to specify the number of bits: int8,int16,int32,int64
Integers by default can have negative values, for positive only you need uint8, uint16, uint32 or uint64.
package main
import "fmt"
func main() {
var a int = 19
b := 36
fmt.Println("a is", a, " b is", b)
}
Floats
For floats, you have float32 and float64. You can define more than one float on one line. Go needs := to assign, not just =. A semicolon at the end of the statement is not required.
package main
import (
"fmt"
)
func main() {
a, b := 1.5, 8.5
fmt.Printf("a %f b %f\n", a, b)
}
This outputs:
a 1.500000 b 8.500000
Program exited.
For less digits after the dot, use %.2f, %.3f etc.
fmt.Printf("a %.2f b %.2f\n", a, b)
Booleans
There's bool, which is True or False. This may be familiar. This is perhaps the only data type which is similar in all programming languages.
In short: only two values are possible, True (1/high) or False (0/low).
a := false
b := true
fmt.Println("a:", a, "b:", b)
Related links:
Top comments (0)