×

go · level 1 · unit 2 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

waits for an answer and stores it.

Programs get more fun when they listen. Ask a question with , which works like Println but stays on the same line. Then fmt.Scan waits for the person to type and press Enter. What they type is .

Scan needs a box to put the answer in. Make an empty one first with name := "", then write fmt.Scan(&name). The & hands Scan the box itself, so it can put the answer inside.

Scan reads one word at a time. If someone types two words, the second one waits for the next Scan.

package main
import "fmt"
func main() {
	name := ""
	fmt.Print("What is your name? ")
	fmt.Scan(&name)
	fmt.Println("Hello,", name)
}

What the person types is not part of the output, so the reply appears right after the question.