Loading
0x10Lesson 2 of 6

Name values and choose types

Store numbers, text, and true/false values.

14 min 4-question quiz 1 code exercise
By the end of this lesson you can
  • Declare and use basic Go values.

Go is statically typed. A short declaration such as count := 3 creates a variable and infers its type. Common types include int, float64, string, and bool. Use var when you want an explicit type or declaration without an initial value.

main.go
1package main
2
3import "fmt"
4
5func main() {
6    language := "Go"
7    year := 2009
8    ready := true
9    fmt.Println(language, year, ready)
10}
Output
Go 2009 true

Variables declared with := are local to the function. Go does not silently convert between different numeric types; choose a type deliberately and convert explicitly when needed.

Key takeaways

  • Declare and use basic Go values.

  • Go catches many mistakes while compiling.

  • Use small experiments to understand each new language feature.

Lesson quiz

4 questions · pass with 3 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write Go

Write Go in the editor and run it against sample input with a remote compiler. Your code is sent to Compiler Explorer to compile and run.

Exercise 1

Double an integer

+25 XP

Read one integer from standard input and print twice its value. fmt.Scan can read the input into an integer variable.

  • Input 4
  • Input -3
main.go
Loading editor…

Go is compiled and run by Compiler Explorer (godbolt.org), a free public service. Your source code and test input are sent to that service.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…

Did you like the lesson? 😆👍
Consider a donation to support our work: