The Building Blocks
The Building Blocks
Every value in Go has a type. These four cover most of what you'll write:
| Type | What it holds | Example |
|---|---|---|
int |
Integers | 42, -7, 0 |
float64 |
Decimals | 3.14, 0.5 |
string |
Text (in quotes) | "hello", "Go" |
bool |
True or false | true, false |
You can do math with numbers, compare them, and glue strings together:
fmt.Println(10 + 3) // 13
fmt.Println(10 > 3) // true
fmt.Println("Go" + "lang") // Golang
fmt.Println(7 / 2) // 3
fmt.Println(7.0 / 2.0) // 3.5
Comparisons (>, <, >=, <=, ==, !=) always produce a bool. The + operator adds numbers or joins strings. When you divide one int by another, Go drops the remainder, which is why 7 / 2 prints 3. To get 3.5, write the numbers as decimals: 7.0 / 2.0.
Spaces when printing multiple values
fmt.Println inserts a space between each argument. + joins strings with nothing between them. If your string already ends in a space and you then use a comma, you get two spaces:
fmt.Println("a", "b") // a b
fmt.Println("a" + "b") // ab
fmt.Println("Status: ", "OK") // Status: OK (two spaces)
fmt.Println("Status: " + "OK") // Status: OK (one space)
Unused variables
Every declared variable must be used, or the compiler stops with an error like declared and not used.
Exercise: Health Check
You're building an API health endpoint. Finish the three fmt.Println calls so the output matches:
Status: OK
Healthy: true
Latency: 1.5Each line uses a different technique:
- Status - join two strings with
+ - Healthy - use
==to comparestatusCodeto200(this produces abool) - Latency - the starter prints
1. Change the numbers so it prints1.5