Introduction
Go, also called Golang, is a programming language made at Google in 2009. It is small, fast and easy to read. Many tools you may know are written in Go, such as Docker and Kubernetes, and many companies use it for web servers and cloud software. Go is a good first "serious" language because there are few keywords and few ways to write the same thing. In this course you will go from your first program to structs, interfaces, errors and goroutines. Every example runs in your browser.
How it works
A Go program is made of packages. A file starts with the name of its package. A package called main is special: it builds into a program that you can run, and it must contain a function called main. That function is the place where the program starts.
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
The import line makes the fmt package available. The name fmt is short for "format". Its functions print text and read input. A function from a package is called with the package name, a dot, and the function name, as in fmt.Println. Names that start with a capital letter are visible from other packages; that is why it is Println and not println.
Go is a compiled language. The compiler reads your whole file and checks it before anything runs. If something is wrong, you get an error with a line number and nothing runs. Go is also strict: an import you never use, or a variable you declare and never use, is an error. This keeps programs clean. On this site the compiler runs in your browser when you press Run.
Three functions print. fmt.Println(a, b) prints its values with a space between them and ends the line. fmt.Print(a, b) prints without ending the line. fmt.Printf(format, values...) fills a text with values. The most useful format verbs are %d for a whole number, %s for text, %.2f for a number with two decimals, %v for any value in its default form, %q for text in quotes, and %T for the type of a value. Printf does not add a new line, so end the text with \n.
A text in double quotes is a string. // starts a comment that the compiler ignores. Go code is normally indented with a tab, and the tool gofmt formats code in one standard style, so Go programs look alike everywhere.
Example
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
name := "Ada"
age := 36
fmt.Print("Name: ")
fmt.Println(name)
fmt.Printf("%s is %d years old\n", name, age)
fmt.Printf("Pi is about %.2f\n", 3.14159)
fmt.Printf("%v and %q\n", name, name)
}Line by line
package mainandimport "fmt"tell Go that this is a program and that it uses fmt.mainstarts by printing a greeting withPrintln.name := "Ada"andage := 36create two variables. The next lesson explains:=in detail.PrintwritesName:and leaves the line open, so the followingPrintlncontinues on the same line.Printffills%swith the name and%dwith the age, and\nends the line.%.2frounds a decimal number to two places.%vprints a value in its default form and%qadds quotes.
Expected result
Hello, Go!
Name: Ada
Ada is 36 years old
Pi is about 3.14
Ada and "Ada"
In the real world
Go programs build into one file with no extra runtime to install, so teams copy that file to a server and run it. Companies use Go for web servers, command-line tools and cloud software. The go build command compiles a project, go run compiles and runs it, and gofmt formats it. Because Go refuses to compile unused variables and imports, code reviews rarely have to discuss small clutter.
Common mistakes
- Forgetting to import fmt, or importing a package you never use. Both are compile errors.
- Using a lower case name, such as
fmt.println. Only capitalised names can be used from another package. - Forgetting
\nin Printf, so the next output runs on the same line. - Using single quotes for text. Single quotes are for one character (a rune); text needs double quotes.
Best practices
- Read the first compile error first. Later errors are often caused by it.
- Let
gofmtformat your code, and indent with tabs. - Use Printf when numbers need a fixed number of decimals, and Println for simple lines.
Summary
A Go program is a file in package main with a func main. The compiler checks the whole file and rejects unused imports and variables. Println, Print and Printf from the fmt package write output, and Printf format verbs such as %d, %s and %.2f put values into a text.
Quick check
Key takeaways
- Every Go program is package main with a func main, and the file must import every package it uses.
- The compiler checks the whole file first; unused imports and variables are errors, not warnings.
- Println adds spaces and a new line, Print adds neither, and Printf fills format verbs such as %d and %s.
Practice problems for this lesson
Was this lesson clear?
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.