Step 1 of 5 · Reading · ~3 min
Learn
Getting Started
Hello, World
Every Go program has the same four-part skeleton, and fmt.Println is how you get anything out of
it. By the end of this lesson you can write that skeleton from memory and predict, character for
character, what a print call puts on the screen.
The skeleton
Output:
compiling
Four pieces, each load-bearing:
package main- every Go file belongs to a package. The package namedmainis the one that builds a runnable program; any other name builds a library for other code to import.import "fmt"- pulls in the standard library's formatting package. Printing lives infmt.func main()- the entry point. Execution starts at the first line of this exact function and the program exits when that function returns.fmt.Println(...)- prints its arguments, then a newline. The capitalPmatters: in Go a capitalized name is exported (usable from other packages) and a lowercase one is private, sofmt.printlndoes not exist at all.
Println inserts its own space
Println accepts any number of arguments. It prints them separated by a single space and ends the
line for you:
Total: 12
x y z
That built-in space is where beginners lose a character. Pad the literal as well and you get two spaces, which is a different string and a failed test:
Total: 12
Count the gap: Total: then two spaces then 12. The comparison is exact, so that is wrong while
Total: 12 is right. The rule is simple - if the value is a separate argument, let Println supply
the space.
Print does not end the line
fmt.Print is the same idea without the trailing newline, and between two string arguments it adds
no space either:
onetwo
Both calls wrote to the same line because neither ended it. Swap them for Println and you get
one and two on separate lines. When output has to be several lines, every line but possibly the
last needs its own line ending, and Println is the call that guarantees it.
The compiler refuses dead code
The same strictness applies to a variable you create and never use (the next lesson introduces variables). These are errors, not warnings: Go will not produce a binary. It feels strict on the first day and it is the reason Go files do not accumulate a graveyard of unused imports.
Common mistakes
- Padding a literal that
Printlnwill already pad.fmt.Println("Total: ", 12)printsTotal: 12with two spaces whereTotal: 12was wanted. - Using
Printwhere a line must end. Two values printed withPrintland on one line -onetwoinstead ofoneandtwo- so every line after the first goes missing. - Changing capitalization or punctuation.
Total: 12,total: 12andTotal:12are three different strings, and only one of them matches. - Printing values in a different order than they arrived. Nothing in the compiler notices; only the output comparison does.
Your exercise
Greet two people, one greeting per line, in the order the names arrive. The starter already reads
both names into first and second, so what you add is the printing. The likeliest way to fail is
the space after the comma: write it in the literal and let the print call add its own and you have
shipped two.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…