Go by Example: Values
Understanding Go's basic value types is crucial for writing robust applications. This code example explores strings, integers, floats, and booleans, demonstrating how to work with literals, perform arithmetic and logical operations, and understand the behavior of Go's strongly-typed system.
Code
package main
import "fmt"
func main() {
// Strings can be concatenated with +
fmt.Println("go" + "lang")
// Integers and floats support arithmetic
fmt.Println("1+1 =", 1+1)
fmt.Println("7.0/3.0 =", 7.0/3.0)
// Booleans with logical operators
fmt.Println(true && false)
fmt.Println(true || false)
fmt.Println(!true)
}Explanation
Go provides a rich set of basic value types including booleans (bool), strings, and various numeric types (integers and floating-point numbers). These fundamental types are the building blocks for all Go programs and understanding their behavior is crucial for writing robust applications.
Strings in Go are immutable sequences of bytes, UTF-8 encoded by default. String concatenation using the + operator creates a new string rather than modifying either original string. The len() function returns the byte length, not character count—a crucial distinction for Unicode text where characters can span multiple bytes.
Numeric types include both signed integers (int8, int16, int32, int64, int) and unsigned integers (uint8, uint16, uint32, uint64, uint). Floating-point types are float32 (single-precision, ~6-7 decimal digits) and float64 (double-precision, ~15 decimal digits, the default). Integer division truncates the decimal part (7/3 = 2), while float division preserves it (7.0/3.0 ≈ 2.333).
Boolean logical operators use short-circuit evaluation for both efficiency and safety: with AND (&&), if the first operand is false, the second isn't evaluated; with OR (||), if the first is true, the second isn't evaluated. This prevents unnecessary computation and potential runtime errors.
Important characteristics of Go's type system:
- No implicit type conversions—prevents subtle bugs at compile time
- Zero values for uninitialized variables (0 for numbers, false for bools, "" for strings)
- Type inference available with
:=operator - Strict typing catches errors early in development

