Your first Go program

package main

Create a new folder named hello and save the following text as main.go inside it.

The first line declares package main, which tells the compiler that this code builds an executable program and not a library.

package main import "fmt" func main() { fmt.Println("Hello, Go course") }

func main

Execution always starts in the main function, which takes no arguments and returns nothing.

The body calls Println from the fmt package to print one line of text, and the opening brace stays on the same line as required by Go.

go run

The go run command compiles the sources into a temporary binary, starts it at once, and removes it after the run.

It is perfect for quick experiments while learning.

go run main.go

Hello, Go course

go build

The go build command produces a permanent binary file that can be started any number of times without the compiler.

go build -o hello main.go

./hello

Hello, Go course

Use go run during development and go build when the program is ready to share or deploy.

Next: Modules with go mod

Article author: Arthur Isaev

Banner image