Variables, Types, and Constants
30 minGo has strong typing with basic types: int, float64, string, bool. Go's type system prevents many common errors by catching type mismatches at compile time. The language has various integer types (int, int8, int16, int32, int64, uint, etc.) for different use cases, though int and int64 are most common. Float types include float32 and float64, with float64 being the default. Understanding type sizes and when to use each type is important for efficient Go programming.
Variables can be declared with var or := shorthand. The var keyword allows explicit type declaration or type inference. The := operator (short variable declaration) infers the type from the assigned value and can only be used inside functions. Multiple variables can be declared together, and Go supports parallel assignment. Understanding when to use each declaration style helps write idiomatic Go code.
Constants are declared with const and cannot be changed. Constants can be typed or untyped, with untyped constants having more flexibility. The iota keyword creates incrementing constants, useful for creating enumerated types. Constants are evaluated at compile time, making them efficient. Understanding constant declarations and iota is essential for writing idiomatic Go code.
Go supports type inference and explicit type conversion. Type inference allows the compiler to determine types from context, reducing boilerplate. However, Go doesn't have implicit type conversion—you must explicitly convert between types. This prevents bugs but requires more verbose code. Understanding type conversion rules helps avoid compilation errors.
Zero values in Go are automatically assigned to variables that are declared but not initialized. Each type has a zero value: 0 for numbers, false for bool, "" for string, nil for pointers/slices/maps/channels/functions/interfaces. This eliminates the need to explicitly initialize variables in many cases. Understanding zero values helps write cleaner, more efficient Go code.
Type aliases and type definitions allow you to create new types based on existing ones. Type definitions create completely new types, while type aliases are just alternative names. This enables type safety—you can't accidentally mix different types even if they have the same underlying type. Understanding these concepts helps design better APIs and prevent bugs.
Key Concepts
- Go has strong static typing with various numeric, string, and boolean types.
- Variables can be declared with var or := (short declaration).
- Constants are immutable and can use iota for incrementing values.
- Go requires explicit type conversion—no implicit conversions.
- Zero values are automatically assigned to uninitialized variables.
Learning Objectives
Master
- Declaring variables with var and := syntax
- Understanding Go's type system and zero values
- Working with constants and iota
- Performing explicit type conversions
Develop
- Understanding type safety and its benefits
- Writing idiomatic Go code with proper type usage
- Designing types for better code safety
Tips
- Use := inside functions for concise variable declaration.
- Use var for package-level variables or when you need explicit types.
- Use iota for creating enumerated constants.
- Be explicit about type conversions to avoid confusion.
Common Pitfalls
- Trying to use := at package level (it's only for function scope).
- Assuming implicit type conversion (Go requires explicit conversion).
- Not understanding zero values, causing unexpected behavior.
- Using wrong integer types, causing overflow or inefficiency.
Summary
- Go has strong static typing with explicit type conversion.
- Variables can be declared with var or := shorthand.
- Constants are immutable and support iota for sequences.
- Zero values eliminate the need for explicit initialization.
Exercise
Demonstrate different variable declarations, types, and constants.
package main
import "fmt"
const (
PI = 3.14159
Language = "Go"
)
func main() {
// Variable declarations
var name string = "Alice"
var age int = 30
var height float64 = 1.75
// Short variable declaration
city := "New York"
isStudent := true
// Multiple variable declaration
var (
firstName = "John"
lastName = "Doe"
email = "john@example.com"
)
// Type conversion
var intValue int = 42
var floatValue float64 = float64(intValue)
fmt.Printf("Name: %s, Age: %d, Height: %.2f
", name, age, height)
fmt.Printf("City: %s, Student: %t
", city, isStudent)
fmt.Printf("Full name: %s %s
", firstName, lastName)
fmt.Printf("Email: %s
", email)
fmt.Printf("Int: %d, Float: %.2f
", intValue, floatValue)
fmt.Printf("Constants - PI: %.5f, Language: %s
", PI, Language)
// Zero values
Exercise Tips
- Experiment with iota for enumerated constants: const ( Red = iota; Green; Blue ).
- Try type conversion between different numeric types: var f float64 = float64(42).
- Use multiple variable declaration for related variables.
- Understand zero values: var s string (empty string), var n int (0).