Skip to content
Lesson 1 of 14

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

go

Output:

compiling

Four pieces, each load-bearing:

  • package main - every Go file belongs to a package. The package named main is 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 in fmt.
  • 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 capital P matters: in Go a capitalized name is exported (usable from other packages) and a lowercase one is private, so fmt.println does 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:

go
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:

go
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:

go
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

go

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 Println will already pad. fmt.Println("Total: ", 12) prints Total: 12 with two spaces where Total: 12 was wanted.
  • Using Print where a line must end. Two values printed with Print land on one line - onetwo instead of one and two - so every line after the first goes missing.
  • Changing capitalization or punctuation. Total: 12, total: 12 and Total:12 are 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.

Up nextVariables and TypesGetting Started

Discussion

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

Sign in to post a comment or reply.

Loading…