Basic Data Types
In this page:
Integers
Go provides both a general-purpose int (whose size matches the platform, 32 or 64 bit) and explicitly sized integer types like int8, int16, int32, and int64, plus unsigned versions (uint8, uint32, etc.) for values that are never negative.
Note: Use plain int unless you have a specific reason (like memory layout or binary formats) to pick a sized variant.
Example: Integers
package main
import "fmt"
func main() {
var count int = 42
var small int8 = 120
var big int64 = 9000000000
fmt.Println(count, small, big)
}
Login to try C/C++/Java/PHP code in the editor
Floating-Point Numbers
float32 and float64 represent numbers with a decimal point. float64 is the default and generally preferred for precision unless memory is tightly constrained, since float32 loses accuracy for very large or very precise values.
Example: Floating-Point Numbers
package main
import "fmt"
func main() {
var price float64 = 19.99
var discount float32 = 0.15
fmt.Println("price:", price, "discount:", discount)
}
Login to try C/C++/Java/PHP code in the editor
Booleans
The bool type holds exactly one of two values, true or false, and is the result of every comparison operator (==, <, >, etc.). Booleans control branching logic in if statements and loop conditions.
Example: Booleans
package main
import "fmt"
func main() {
isActive := true
hasExpired := false
fmt.Println("active:", isActive, "expired:", hasExpired)
}
Login to try C/C++/Java/PHP code in the editor
Strings
A string in Go is an immutable sequence of bytes, most commonly holding UTF-8 encoded text. String literals are written in double quotes, and Go provides many built-in operators and standard-library functions for working with them.
Example: Strings
package main
import "fmt"
func main() {
greeting := "Namaste"
fmt.Println(greeting, "length in bytes:", len(greeting))
}
Login to try C/C++/Java/PHP code in the editor
- Using int everywhere and assuming it's always 64-bit -- its size actually depends on the target platform (32 or 64 bit).
- Mixing float32 and float64 values in the same expression without an explicit conversion, which Go's type system rejects.
- Comparing floating-point numbers with == for equality when rounding error can make two equal values differ slightly.
- Go's core basic types include int, float64, bool, and string.
- Integer types come in sized variants like int8, int16, int32, int64 and their unsigned counterparts.
- float32 and float64 represent decimal numbers, with float64 the common default.
- Go is strict: it never silently mixes types in an expression without an explicit conversion.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: