Understand the contract

A runnable Go command uses package main and a main function without parameters or return values. Save the example as main.go inside the module created in the installation lesson. Execute go run . from that directory. The program prints one greeting and then exits when main returns.

The import declaration makes the fmt package available in this file. Println is an exported function, so its name begins with a capital letter. Go checks imports per file: importing fmt in another file does not make it available here. Unused imports are compile errors.

Work through the example

Println writes its arguments followed by a newline. The string is enclosed in double quotes, and the braces delimit the function body. Go inserts many semicolons automatically, so everyday Go code rarely writes them explicitly. Keep the opening brace on the same line as the function signature.

package main

import "fmt"

func main() {
    fmt.Println("Hello, Go!")
}

Watch this boundary

A file named main.go does not become executable solely because of its filename. Its package and entry function determine that behavior. Renaming main to greet leaves package main without the required entry point; changing the package to hello turns it into a library package.

Practice with a changed input

Print two lines, then remove the fmt import and read the compiler diagnostic. Restore it and run go fmt . before moving on to modules.

References & further reading

Need help with your Go backend?

I work with engineering teams on focused Go and Kubernetes platform engagements, with limited consulting availability.

Explore Go consulting services