beginner
Step 2 of 15
Variables and Data Types
Go Programming
Variables and Data Types
Go is a statically typed language, meaning every variable has a fixed type determined at compile time. Go provides several ways to declare variables and a rich set of built-in types including integers of various sizes, floating-point numbers, strings, booleans, and composite types like arrays and structs. Understanding Go's type system is fundamental because the compiler enforces type safety strictly — there is no implicit type conversion, and unused variables cause compilation errors.
Variable Declaration
package main
import "fmt"
func main() {
// Method 1: var keyword with type
var name string = "Alice"
var age int = 30
// Method 2: var with type inference
var price = 19.99 // float64 inferred
var active = true // bool inferred
// Method 3: Short declaration (most common, inside functions only)
city := "New York"
count := 42
// Multiple declarations
var (
firstName string = "Bob"
lastName string = "Smith"
score int = 95
)
// Constants
const Pi = 3.14159
const MaxSize = 100
const AppName = "MyApp"
// iota for enumerations
const (
StatusPending = iota // 0
StatusActive // 1
StatusClosed // 2
)
// Zero values (default when not initialized)
var i int // 0
var f float64 // 0.0
var s string // ""
var b bool // false
fmt.Println(name, age, price, active, city, count)
fmt.Println(firstName, lastName, score)
fmt.Println(i, f, s, b)
}
Basic Data Types
package main
import (
"fmt"
"math"
)
func main() {
// Integers
var i8 int8 = 127 // -128 to 127
var i16 int16 = 32767
var i32 int32 = 2147483647
var i64 int64 = 9223372036854775807
var u8 uint8 = 255 // 0 to 255 (byte is alias for uint8)
// int is platform-dependent (32 or 64 bit)
var n int = 42
// Floating point
var f32 float32 = 3.14
var f64 float64 = math.Pi // Default for float literals
// Strings (immutable UTF-8 sequences)
greeting := "Hello, World!"
multiline := `This is a
raw string literal
with "quotes" and ackslashes\`
// Runes (Unicode code points, alias for int32)
var r rune = 'A' // 65
emoji := '🚀'
// Type conversion (explicit only — no implicit conversion in Go)
x := 42
y := float64(x) // int to float
z := int(y) // float to int (truncates)
s := string(65) // "A" (from rune)
num := fmt.Sprintf("%d", x) // int to string "42"
fmt.Println(i8, i16, i32, i64, u8, n)
fmt.Println(f32, f64)
fmt.Println(greeting, multiline)
fmt.Println(r, emoji, y, z, s, num)
}
Pro tip: Go requires explicit type conversion — float64(myInt) — and will not implicitly convert between types. This prevents subtle bugs but means you need to be deliberate about conversions. Also, Go does not allow unused variables — the compiler will refuse to compile if any variable is declared but never used.
Key Takeaways
- Go is statically typed with no implicit type conversion; use explicit conversions like
float64(x). - Use
:=for short variable declaration inside functions;varfor package-level or explicit-type declarations. - Zero values are meaningful defaults: 0 for numbers, "" for strings, false for booleans, nil for pointers.
- Constants use
constandiotafor auto-incrementing enumerations. - Unused variables cause compilation errors — Go enforces clean code at the compiler level.