Hello, World

Every Go program needs two things to run: a package called main, and a function called main inside that package. When you execute a Go program, the main function is where it starts.

Let's take a look at a simple Go program that prints "Hello, Go!" to the console:

package main

import "fmt"

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

The package declaration

The first line declares that this file belongs to the main package.

The name main is special. When the Go compiler sees it, the folder is built as an executable program rather than a library. Rename that line to package greeting and the file still compiles, but the result is a library that other code can import, with no way to run directly.

What's a Package

A package is a group of related Go files that belong together. It is the first level of organization in a Go program. For now, the important rule is simple: programs live in package main.

The import line

The second line, import "fmt", imports the package name fmt available in this file.

fmt is short for "format". It is one of the standard library packages that come with Go and provides functions for formatting and printing text.

fmt.Println("Hello") // works
Println("Hello")     // compile error: undefined: Println

One of the beauties of Go is that it has a rich standard library, so you can do a lot without needing to install third-party packages. We will be seeing more of the standard library as we go through this course.

Gotcha

You can't import a package and not use it. The Go compiler will throw an error if you have an unused import. This is a common source of frustration for beginners, but it encourages clean code. If you import something, you should be using it.

The main function

Next we have the main function which is the entry point of the program. This is where the Go runtime starts executing your code.

func main() {
}

If the package is main but the function is missing, the build fails with an error function main is undeclared in the main package.

Trying it out

We have a coding environment on the right side where you can edit and try the concepts we will be covering in this course. However, please keep in mind that this is a simplified environment designed for learning purposes. It may not have all the features and behaviors of a full Go development setup on your local machine. Some of the later lessons will ask you to set up Go on your own computer.

Running Go programs locally

If you want to try this example on your own machine, you can create a file called main.go and copy the code into it. Then you have two options to run it:

go run compiles your program, runs it, and throws away the temporary binary when the program exits:

go run main.go

go build compiles your program and keeps the resulting binary in the current folder:

go build main.go

On macOS and Linux, you run that binary with ./main. On Windows, you run main.exe.

Task
  • Change "Hello, Go!" to "Hello, Kamran".
  • Add fmt.Println(2026) on the next line.
Expected output
Hello, Kamran
2026