Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

How to Write and Run Your First Go Program

Create a Hello, World! program in Go, run it from a terminal, and see how package main, fmt, and the main function fit together.

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 main declares an executable package.
  • import "fmt" makes Go’s standard formatting package available.
  • func main() defines the function Go calls when the program starts. fmt.Println writes 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!

How to create basic program in Golang Picture 1

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.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.