Go Get Started
Getting started with Go is the path from a written file to a running program. The go tool compiles the file and then runs it.
The full shape
This program shows the full shape. Each part has one job.
package main
import "fmt"
func main() {
fmt.Println("Let's get started.")
}Two statements in order
Two print statements sit inside main. Each statement prints one line.
package main
import "fmt"
func main() {
fmt.Println("First step")
fmt.Println("Second step")
}Example
package main
import "fmt"
func main() {
fmt.Println("Program started.")
fmt.Println("Program finished.")
}Key points
- A Go program in this course lives in a file named main.go.
- Use go run main.go to compile a program and run it.
- Use gofmt to format Go code in the standard style.
- package main names the package that holds the program.
- The import line brings in the fmt package for printing.
- func main is where running starts.
- Each statement runs in order from top to bottom.
In this example, the first statement prints Program started. The second statement prints Program finished. The lines appear in the order of the code.
Note
gofmt writes indentation with tab characters.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท Go Playground
Exercise
Which command compiles a program and runs it?