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
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
=. Writingattempts := 1and thenattempts := 2in the same block is a compile error;attempts = 2is 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:
So this program is fully defined, and prints three lines:
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.
== 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
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:
Reading input
fmt.Scan fills variables from standard input, skipping spaces and newlines:
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
9and2in that order, the difference is7; printing-7means the operands were swapped. Subtraction is not symmetric, unlike addition. - Deciding equality from the arithmetic. Two values that sum to
0are not equal:-6and6sum to0while being different numbers. Compare the two variables themselves with==rather than testing a computed number. - Inventing your own boolean words.
Yes,TrueandEQUALare all wrong output. A printed Goboolis exactlytrueorfalse, lowercase. - Putting everything on one line.
fmt.Printwrites no newline, sofmt.Print(40)followed byfmt.Print(41)produces4041on 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.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…