Basic Data Types
In this page:
var name int // int, int8..int64, uint, uintptr
var name float64 // float32, float64
var name bool
var name string
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.
Example: Integers
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Declare int and set it to 42
var count int = 42
// Declare int8 and set it to 120
var small int8 = 120
var big int64 = 9000000000
// Print the values followed by a newline
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
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Declare float64 and set it to 19.99
var price float64 = 19.99
// Declare float32 and set it to 0.15
var discount float32 = 0.15
// Print the values followed by a newline
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
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create isActive and infer the type
isActive := true
// Short declaration: create hasExpired and infer the type
hasExpired := false
// Print the values followed by a newline
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
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create greeting and infer the type
greeting := "Namaste"
// Print the values followed by a newline
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: