A program is a list of instructions for a computer, written as text. Go is a programming language: a set of rules for writing those instructions so that a computer can follow them. To run a program is to ask the computer to carry the instructions out.
You do not need to install anything for these lessons. Every example with an Edit & run button runs right here on the page, and you can change it and run it again. Nothing you type can break anything.
The smallest useful program
Here is a complete Go program. Run it.
package main
import "fmt"
func main() {
fmt.Println("Hello, world!")
}It shows Hello, world! on the screen. The text a program shows is called its output.
Only one line of that program does the visible work. The lines around it are a frame that every Go program in this unit has. You will copy the frame each time, so here is what each part says:
package mainsays "this file is a program that can be run".import "fmt"asks for a package: a named collection of ready-made tools. The packagefmt(short for "format") holds the tools for showing text.func main() {and the closing}on the last line mark the main function. A Go program always starts at the first line between those curly brackets and stops when it reaches the closing one.
Your instructions go between the curly brackets. Lesson 6 explains func properly; until then, treat the frame as fixed.
What the working line says
fmt.Println("Hello, world!")fmt.Printlnis a function: a named action that Go already knows how to do. The name reads "thePrintlntool from thefmtpackage", andPrintlnis short for "print line". It shows text on the screen and then ends the line.- The round brackets after the name call the function, which means "do that action now".
"Hello, world!"is a string: a piece of text. The double quotation marks tell Go where the text starts and stops. They are not part of the text, so they are not shown.
The value you put between the round brackets is called an argument. It is what the function works on: here, the text to show.
From text to a running program
Go does not run your text directly. A tool called the compiler first reads the whole program and checks every line against the rules of the language. Only when every line passes does it build something the computer can run. On this page that happens each time you press the button, and it takes a moment.
fmt.Println puts a line on the screen.Statements run from top to bottom
A statement is one complete instruction. In Go a statement normally takes one line. A program with three statements inside main does three things, always in the order they are written: first line first.
package main
import "fmt"
func main() {
fmt.Println("Fill the kettle")
fmt.Println("Boil the water")
fmt.Println("Make the tea")
}This prints three lines, in the same order as the statements:
Fill the kettle
Boil the water
Make the teaStep through the diagram to watch each line run. Then swap two of the fmt.Println lines in the program above and run it again: the output changes order too, because Go never rearranges your statements.
fmt.Println starts a new line on the screen.fmt.Println() with nothing between the round brackets prints an empty line. That is useful for spacing output out.
Comments are notes for people
A comment starts with //. Go ignores everything from the // to the end of that line. Comments are for the person reading the program, including you next week.
package main
import "fmt"
func main() {
// Show a two-line welcome.
fmt.Println("Welcome!")
fmt.Println() // an empty line
fmt.Println("Come in.")
}This prints Welcome!, then an empty line, then Come in.. The comments print nothing.
A // inside the quotation marks of a string is ordinary text, not a comment: fmt.Println("Open // closed") prints Open // closed.
Exactness matters
Go follows your text exactly, so small differences change the result:
- Capital letters count.
fmt.Printlnworks;fmt.printlndoes not exist. Every tool you take from a package starts with a capital letter. - Everything between the quotation marks is printed exactly, including spaces and punctuation.
"Hello,world!"and"Hello, world!"are different strings. - A string needs a double quotation mark at both ends, and each call needs both round brackets.
- The opening curly bracket belongs at the end of the
func main()line, not on a line of its own.
The spaces at the start of the lines inside main are called indentation. Go does not need them, but everyone writes them, because they show at a glance which lines are inside the curly brackets.
Read your first error message
Everyone who programs sees error messages every day. An error message is not a telling-off. It is the compiler saying which line it could not accept, and why. Learning to read one is a skill in its own right.
This program has a mistake: the closing quotation mark is missing.
package main
import "fmt"
func main() {
fmt.Println("Hello, world!)
}The compiler refuses to build it, so nothing runs, and it reports:
# command-line-arguments
./main.go:6:32: newline in string
./main.go:6:32: syntax error: unexpected newline in argument list; possibly missing comma or )Read it like this:
- Skip the line that starts with
#. It is a heading, not a problem. - Each other line starts with a place.
./main.go:6:32:means the filemain.go(the name this site gives your program), line 6, and character 32 along that line. Line 6 is the one to look at. - After the place comes the problem. "Newline in string" means the line ended while a string was still open: Go was still waiting for the closing quotation mark.
- Fix the first problem first. The second message is a knock-on effect of the first. Once the quotation mark is back, both disappear.
A second very common mistake is a misspelt name:
package main
import "fmt"
func main() {
fmt.println("Hello, world!")
}# command-line-arguments
./main.go:6:9: undefined: fmt.println (but have Println)"Undefined" means Go met a name it does not know. The place says line 6, and here the compiler even suggests the fix: the package has Println, with a capital P.
When you see an error: find the first line with a place, go to that line number in your program, read what the problem is, and compare that spot with a working example. Because the compiler checks everything before anything runs, a Go program with a mistake like these prints nothing at all, not even the lines above the mistake.
Running Go on your own computer
This is optional; the lessons do not need it. If you install Go from go.dev, you can save a program in a text file named hello.go, open a terminal (a window where you type commands) in the same folder and type go run hello.go. The output appears in the terminal, the same as it does on this page.
What you can do now
You can write a complete Go program of several fmt.Println statements, predict the order of its output, add comments, and find the line an error message is pointing at. In the exercises you will predict what small programs print, repair two broken ones and then write a three-line program of your own.
Next: name values with variables, so that a program can remember and calculate things.