Go Hello World Program


Writing Your First Go Program

Create a file named hello.go. In Go, the filename typically reflects the purpose of the program. Here, we use hello.go for our Hello World example.

package main

import "fmt"

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

Compiling the Program

Open a terminal where your hello.go file is saved and run the following command:

go build hello.go

What happens during compilation?

The go build command compiles the Go source code:

  • Parses your .go source file written in Go syntax.
  • Compiles it to machine code.
  • Generates an executable named hello (or hello.exe on Windows).

This binary is now ready to be executed.

Running the Program

To run the compiled program, use the following command in your terminal:

./hello

What happens during execution?

The system loads and runs your Go binary:

  • Execution begins from the main() function.
  • fmt.Println prints Hello, World! to the console.
Hello, World!

You’ve now compiled and executed your first Go program — an essential first step in learning the Go language.

Common Issues to Watch For

  • Incorrect function name: The entry point must be main, not Main or anything else.
  • Missing import: Forgetting to import "fmt" will cause an undefined function error.
  • Go file naming: The file must end in .go and be placed in a proper Go workspace (if modules are used).

Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...