Golang hello world: first program, go mod init, and go run

Golang hello world and go hello world: hello world golang program with package main, hello world in golang and go language hello world using fmt.Println, golang hello world example with go mod init, go run and go hello world example, hello world in go and go hello world program on Linux.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

Golang hello world: first program, go mod init, and go run

A golang hello world or go hello world program is the usual first step: print a line, learn package main, and run with the go tool. People also search hello world golang, hello world in golang, go language hello world, helloworld golang, golang hello world example, go hello world example, hello world in go, and go hello world program—the steps are the same. For a fuller setup guide, see Getting started with Go; for how packages fit together, see golang packaging.

Tested with Go 1.24 on Linux.


Install Go

Follow the official Download and install instructions for your OS, then confirm the toolchain is on your PATH with go version (your version string will differ).

text
go version go1.24.4 linux/amd64

Hello world in Go: module and source file

Create a directory and turn it into a module. The module path can be a placeholder for learning (for example example/hello); published modules should use a path your go proxy can fetch.

bash
mkdir hello && cd hello
go mod init example/hello

You should see a line confirming go.mod was created.

Create hello.go with a main function and the standard library fmt package:

go
// first program
package main

import "fmt"

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

package main tells the toolchain to build a command. func main() is where execution starts. fmt.Println writes a line to standard output.

Running go run . from that directory prints Hello, world! on its own line.


Run and explore the go command

Common commands for a go hello world workflow:

bash
go run .
go build -o hello
./hello

go run compiles and runs without leaving a binary in the current directory (unless you use -o). go build produces a binary you can run repeatedly.

go help lists subcommands; the start of the output looks like:

text
Go is a tool for managing Go source code.

Usage:

	go <command> [arguments]

The commands are:

	bug         start a bug report
	build       compile packages and dependencies
	clean       remove object files and cached files
	doc         show documentation for packages or symbols

Summary

This golang hello world walkthrough matches hello world golang and go hello world searches: install Go, run go mod init once per project, put package main and func main in a .go file, call fmt.Println for a classic line, and use go run . or go build for a go hello world program you can extend. That is the same core path for hello world in golang, go language hello world, golang hello world example, go hello world example, hello world in go, and helloworld golang—one small file, one module file, and the official go driver.


References


Frequently Asked Questions

1. What is the smallest golang hello world or go hello world program?

A file in package main with func main() and a call such as fmt.Println("Hello, world!"), plus a go.mod from go mod init when you treat the folder as a module.

2. How do I run a go hello world example?

From the directory that contains go.mod and your .go file, run go run . (or go run hello.go); the compiler builds a temporary binary and runs it.

3. Why does hello world in golang use package main?

package main marks an executable command; the main function is the entry point. Library packages use a different package name and are built with go build as archives, not standalone commands.

4. What does go mod init do for hello world in go?

It writes a go.mod file with a module path so the Go toolchain can record dependencies later; for a tiny hello program you may have no imports beyond the standard library.

5. go run vs go build for a go hello world program?

go run compiles and executes in one step; go build writes a binary you run yourself (for example ./hello), which is closer to how you ship production tools.
Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise …