Hello, Go
Write and run your very first Go program: package main, imports, fmt.Println.
Hello, Go
You're about to write your first Go program — no installs, no compilers to download. Every exercise on AprendeGO runs a real Go interpreter right in your browser, so the only thing between you and your first fmt.Println is a click on Run.
Anatomy of a Go program
Every standalone Go program follows the same skeleton:
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
Three parts matter here:
package main— every program that can run by itself is amainpackage. (Later you'll meet packages that get imported by others; they are notmain.)import "fmt"— brings thefmtpackage into your program.fmtis short for format: it holds the functions that print text.func main()— the entry point. When your program runs, the machine looks formainand executes it from top to bottom.
The program above prints one line:
Hello, Go!
Exercise hg-1 has this skeleton ready — press Run and then make it print the message.
Printing with fmt.Println
fmt.Println prints its arguments followed by a newline:
fmt.Println("Vinyl Club") // one string
fmt.Println(1999) // one number — no quotes needed
fmt.Println("Vinyl Club", 1999) // several values, separated by spaces
Vinyl Club
1999
Vinyl Club 1999
Notice the difference between "1999" and 1999: quotes make a string (text), while a bare number is a number, printed exactly as its value.
Formatting with fmt.Printf
fmt.Printf does not print values directly. Instead it takes a format string containing format verbs — placeholders that get replaced by the arguments:
| Verb | Meaning | Example | Output |
|---|---|---|---|
%s | a string | fmt.Printf("%s est. %d", "Vinyl Club", 1999) | Vinyl Club est. 1999 |
%d | a number | fmt.Printf("%d artists", 12) | 12 artists |
%v | any value | fmt.Printf("%v", 3.14) | 3.14 |
Verbs appear in the format in the exact order of the arguments that fill them:
fmt.Printf("Today's pick: %s (%d)\n", "Midnight Express", 1999)
Today's pick: Midnight Express (1999)
Two details that trip everyone up:
Printfdoes not add a newline. Write\nat the end of the format string yourself. In Go source code,\nis a single character: the newline.- The number of verbs must match the number of arguments. Missing one is a compile error.
Comments: // and /* */
Comments are notes for humans. Go ignores them completely:
// this whole line is a comment
fmt.Println("visible") // a comment can also follow code
You can "hide" a line by commenting it out — a classic trick for temporarily disabling code. If a program's output is wrong, ask yourself: is that line a comment by mistake?
The record store
From this chapter on, exercises take place in Vinyl Club, a record store with 12 artists, 17 albums and 49 tracks. You don't need to do anything with the data yet — for now, just knowing the store exists is enough. You'll meet its artists and albums in the next chapters.
What you just learned
- A Go program is
package mainwith afunc main()that runs first. import "fmt"gives you the printing functions.fmt.Println(value)prints and moves to a new line; several values get separated by a single space.fmt.Printf("format", args)uses verbs like%sand%d, and needs an explicit\n.- Numbers are written without quotes; strings go between double quotes.
//comments are ignored by Go.
Next: variables and types — how to store values with var and :=.