Introduction
Go has various value types that form the foundation of all programs. Understanding these basic types - strings, integers, floats, and booleans - is essential for writing Go code.The Code
Value Types in Detail
Strings
+ operator. This example outputs golang.
Strings in Go are immutable - once created, their contents cannot be changed. Operations like concatenation create new strings.
Integers and Floats
- Integer arithmetic:
1+1produces2 - Float arithmetic:
7.0/3.0produces2.3333333333333335
Booleans
&&- AND operator||- OR operator!- NOT operator
Common Value Types
Go provides several built-in value types:| Type | Examples | Description |
|---|---|---|
string | "hello", "golang" | Text data |
int | 42, -7, 1000 | Whole numbers |
float64 | 3.14, 7.0, -2.5 | Decimal numbers |
bool | true, false | Boolean values |
Go has multiple integer types (
int8, int16, int32, int64) and float types (float32, float64). The plain int type is the most commonly used and is sized based on your system (32 or 64 bits).Key Takeaways
- Strings are concatenated with
+ - Go distinguishes between integer and floating-point arithmetic
- Boolean operators work as expected from other languages
- Go is a statically typed language - each value has a specific type
- Use
fmt.Printlnto output values of any type