Table of Contents
To run your first Go program, save the code below as first.go and enter go run first.go from the same folder. You’ll need Go installed and a plain-text editor.
1. Create a Go source file
Open a terminal and enter go version to check that the Go command is available. If your terminal cannot find it, follow the official Go installation instructions and reopen the terminal.
Create first.go in a folder you can find again. On Windows, check that the editor did not save it as first.go.txt. Add this code:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
package maindeclares an executable package.import "fmt"makes Go’s standard formatting package available.func main()defines the function Go calls when the program starts.fmt.Printlnwrites the message and ends it with a newline.
2. Run the program
In the terminal, navigate to the folder containing first.go and run:
go run first.go
You should see:
Hello, World!

go run compiles and runs this file in one step. This single-file example uses only the standard library, so you can run it as shown without setting up a module. If Go reports that first.go cannot be found, check the terminal’s current folder and the file extension.
Try storing the message in a variable before printing it. The guide to values and variables explains that pattern. When you’re ready to store several values together, try arrays in Go.
Reader Comments 0
Sign in with email or Google to join the discussion.