02 - Values and Variables
📋 Jump to TakeawaysPrograms work with data: numbers, text, true/false values. A value is a piece of data. A variable is a name that holds a value so you can use it later, change it, and pass it around.
Values
Go has a few basic types:
42 // an integer (whole number)
3.14 // a float (decimal number)
"Gopher" // a string (text)
true // a boolean (true or false)Every value has a type. 42 is an int (Go's default for whole numbers). 3.14 is a float64 (Go's default for decimals). "Gopher" is a string. true is a bool. Go cares about types. You can't mix them without being explicit about it.
Variables
A variable is a name that holds a value. You can create one with var:
var name string = "Gopher"
var age int = 25
var active bool = trueOr use the short form :=, which lets Go figure out the type:
name := "Gopher"
age := 25
active := true:= is what you'll use most of the time. It's shorter and Go is smart enough to know that "Gopher" is a string and 25 is an int.
The var form is useful when you want to declare a variable without giving it a value yet:
var score int // score is 0
var title string // title is ""
var ready bool // ready is falseZero Values
Go doesn't leave variables empty. If you declare a variable without a value, Go gives it a default called a zero value:
| Type | Zero Value |
|---|---|
int |
0 |
float64 |
0.0 |
string |
"" |
bool |
false |
Variables in Go are always safe to use. No surprises.
Printing Variables
You already know fmt.Println. It can print variables too:
name := "Gopher"
age := 25
fmt.Println(name) // Gopher
fmt.Println(age) // 25
fmt.Println(name, age) // Gopher 25For more control, use fmt.Printf with format verbs:
name := "Gopher"
age := 25
price := 9.99
fmt.Printf("Name: %s\n", name) // Name: Gopher
fmt.Printf("Age: %d\n", age) // Age: 25
fmt.Printf("Price: %.2f\n", price) // Price: 9.99%s is for strings, %d is for integers, %.2f is for floats with two decimal places. \n adds a new line. Printf doesn't add one automatically like Println does.
Changing Variables
Variables can change. That's why they're called variables:
score := 0
fmt.Println(score) // 0
score = 10
fmt.Println(score) // 10
score = score + 5
fmt.Println(score) // 15Notice the second line uses =, not :=. You only use := when creating a new variable. After that, = updates it.
Constants
If a value should never change, use const:
const pi = 3.14159
const appName = "MyApp"Trying to change a constant gives a compiler error. Use constants for values that are fixed for the entire program.
Naming Rules
- Names must start with a letter or underscore
- They can contain letters, digits, and underscores
- Go convention is
camelCase:userName,maxScore,isActive - Short names are fine for short-lived variables:
i,s,n - Descriptive names for anything that lives longer:
totalPrice,userEmail
Basic Math
Go supports the math operations you'd expect:
a := 10
b := 3
fmt.Println(a + b) // 13
fmt.Println(a - b) // 7
fmt.Println(a * b) // 30
fmt.Println(a / b) // 3 (integer division, no decimals)
fmt.Println(a % b) // 1 (remainder)Integer division drops the decimal. 10 / 3 is 3, not 3.33. If you need decimals, use float64:
a := 10.0
b := 3.0
fmt.Println(a / b) // 3.3333333333333335Key Takeaways
- Values have types:
int,float64,string,bool :=creates a new variable and lets Go infer the typevardeclares a variable explicitly, useful when you need a zero value- Go gives every variable a zero value:
0,"",false =updates an existing variable,:=creates a new oneconstdeclares values that never changefmt.Printfwith%s,%d,%fgives you formatted output- Integer division drops decimals. Use
float64when you need them