← Back to the path
01
0/7 exercises

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:

  1. package main — every program that can run by itself is a main package. (Later you'll meet packages that get imported by others; they are not main.)
  2. import "fmt" — brings the fmt package into your program. fmt is short for format: it holds the functions that print text.
  3. func main() — the entry point. When your program runs, the machine looks for main and 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:

VerbMeaningExampleOutput
%sa stringfmt.Printf("%s est. %d", "Vinyl Club", 1999)Vinyl Club est. 1999
%da numberfmt.Printf("%d artists", 12)12 artists
%vany valuefmt.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:

  • Printf does not add a newline. Write \n at the end of the format string yourself. In Go source code, \n is 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 main with a func 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 %s and %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 :=.

Exercises

  1. 01Hello, Go!
  2. 02Two lines
  3. 03Print a number
  4. 04One call, two values
  5. 05Printf: format verbs
  6. 06Comments: turn it on
  7. 07The storefront signboss