Skip to content
Lesson 2 of 14

Step 1 of 5 · Reading · ~4 min

Learn

Getting Started

Variables and Types

Go is statically typed: every variable has one type, fixed when the program is compiled. By the end of this lesson you can declare variables three ways, say what value a variable holds before you assign one, and predict what fmt.Println prints for an int, a string and a bool.

Three ways to declare

go

The short form := is what you will write most of the time, and it has two rules:

  • It declares, so it is used once per name in a scope. Changing the value later uses plain =. Writing attempts := 1 and then attempts := 2 in the same block is a compile error; attempts = 2 is correct.
  • It works inside functions only. A variable declared at the top of a file, outside any function, needs var.

A declared variable that is never read is a compile error too, not a warning. Go would rather refuse to build than ship dead code.

Zero values

A var declaration with no value is not uninitialised memory. Every type has a defined zero value, and the declaration uses it:

go

So this program is fully defined, and prints three lines:

go

Note the last one: Go prints booleans in lowercase, as true and false. That spelling is the value's own, not a style choice you can change.

Booleans come from comparisons

A comparison is an expression: it produces a bool value you can store or print, the same way + produces a number.

go

== compares, a single = assigns; mixing them up is a compile error in Go rather than a silent bug. Comparison works on strings too: "go" == "Go" is false, because the comparison is byte for byte.

Types never convert themselves

go

No implicit conversion, ever, not even between int and int64. Where precision changes, somebody wrote the conversion and a reviewer can see it.

Constants follow the same spirit but are fixed at compile time:

go

Reading input

fmt.Scan fills variables from standard input, skipping spaces and newlines:

go

The & hands Scan each variable's address so it can write into it; pointers get their proper chapter later. For now: Scan needs &, and each value read must already be declared with the type you expect.

Common mistakes

  • Subtracting the other way round. For the values 9 and 2 in that order, the difference is 7; printing -7 means the operands were swapped. Subtraction is not symmetric, unlike addition.
  • Deciding equality from the arithmetic. Two values that sum to 0 are not equal: -6 and 6 sum to 0 while being different numbers. Compare the two variables themselves with == rather than testing a computed number.
  • Inventing your own boolean words. Yes, True and EQUAL are all wrong output. A printed Go bool is exactly true or false, lowercase.
  • Putting everything on one line. fmt.Print writes no newline, so fmt.Print(40) followed by fmt.Print(41) produces 4041 on a single line. Use the call that ends the line after each value.

Your exercise

Read two integers and report three facts about them, one per line, in the order the exercise gives. The starter already declares both variables and reads them with fmt.Scan, so what you add is the printing. The mistake that costs most people a run is the order of the subtraction.

Up nextNumbers and MathGetting Started

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…