go · level 1 · unit 2 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A is a box with a name on it.
A variable is a box with a label. You put a inside, write a name on the label, and from then on you use the name to get the value back.
pet := "Rex" makes a new box called pet and puts the value "Rex" inside. Making a new box is called declaring it. The longer way, var pet = "Rex", does the same job.
Go is tidy about boxes. one and never use it, and Go refuses to run, saying it is declared but not used. Every box needs a job.
package main
import "fmt"
func main() {
pet := "Rex"
var snack = "biscuits"
fmt.Println(pet)
fmt.Println(snack)
} Read := as "make a new box and put this in it".
guess first
Quotes or no quotes? What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
package main
import "fmt"
func main() {
name := "Sam"
fmt.Println(name)
fmt.Println("name")
}
your turn
Your turn. Put your name in the box.
Type your name between the quotes, then press run.
output
press run to see what happens
fix it
Go refuses to run this. Can you fix it?
output
press run to see what happens
- Read the error. Which box does Go say is declared but not used?
- The box called drink holds lemonade, but no line ever shows it.
- Add a line under the Snack line: fmt.Println("Drink:", drink). Then press run.
nailed it
your program can remember things now. :D
- A variable is a box with a name on it.
:=declares a new box and puts a value in it.- Every box you declare must be used.
new words in your collection
- variable
- value
- declare
This lesson isn’t saved. A free account remembers every lesson you finish.