go · level 1 · unit 3 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Every value has a .
Every value in Go has a type, which says what kind of thing it is. A string is text. An is a whole number, like 7. A has a decimal point, like 2.5. A is true or false.
Quotes change everything. "5" + "3" joins two strings into "53", but 5 + 3 adds two numbers to make 8. Mix a string and a number with +, and Go refuses to run.
A box keeps its type for life. age := 12 makes an int box, so later it can hold 13 but never "thirteen". You can also name the type yourself: var age int = 12.
package main
import "fmt"
func main() {
var name string = "Rex"
var age int = 3
var weight float64 = 12.5
var isGoodBoy bool = true
fmt.Println(name, age, weight, isGoodBoy)
} Scan fills a box in its own type, so age := 0 and then fmt.Scan(&age) reads a number.
guess first
Quotes or no quotes? Guess both lines.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
package main
import "fmt"
func main() {
fmt.Println("5" + "3")
fmt.Println(5 + 3)
}
your turn
Your turn. Describe a pet.
Type a pet's name between the quotes, and its age as a whole number with no quotes. Then press run.
output
press run to see what happens
fix it
Go will not add these up. Can you fix it?
output
press run to see what happens
- Read the error. It says the types do not match. Which two boxes are being added?
- apples holds "3" in quotes, which is a string. pears holds a number.
- Remove the quotes around 3 on line 4 so both boxes hold an
int, then press run.
nailed it
you can tell text from numbers now. :D
- Every value has a type:
string,int,float64orbool. - Quotes make a string, even when it looks like a number.
- A box keeps the same type for life.
new words in your collection
- type
- int
- float64
- bool
This lesson isn’t saved. A free account remembers every lesson you finish.