Search and navigate

Pages

Start HereRoadmapTutorialsCheatsheetInterview QuestionsResources

Published tutorials

Building a REST API with Go: The Complete TutorialError Handling in Go: Wrapping, Classification, and APIsFan-Out Fan-In in Go: Parallel Work with ChannelsFuzz Testing in Go: Finding Bugs AutomaticallyGeneric Functions in Go: Writing Reusable CodeGo Benchmarking: Measuring Performance with testing.BGo Channel Patterns: Advanced Recipes That WorkGo Channels: Communication Between GoroutinesGo Concurrency Patterns: A Practical CatalogueGo Constraints: comparable, Ordered and Custom SetsGo Context: Cancellation, Deadlines, and Request ValuesGo defer: Cleanup, Ordering, and Common TrapsGo HTTP Client: Timeouts, JSON, and Reliable RequestsGo HTTP Middleware: Build a Production ChainGo Interview Questions: 25 Answers That Show UnderstandingGo Mutex: Protecting Shared State from Race ConditionsGo net/http: Build an HTTP Server from ScratchGo select Statement: Multiplexing Channels ExplainedGo Tools You Need for a Reliable Development LoopGo Tutorial: From First Program to Backend FoundationsGo Type Parameters: Syntax and Constraints ExplainedGo Worker Pool Pattern: Bounded Concurrency at ScaleGo's Type System Deep Dive: Static Typing Done RightGolang Compilation and Execution ExplainedGolang Developer Salary 2026: US, UK and RemoteGolang File Structure for Modules and ApplicationsGolang Hello World and Your First Go ProgramGolang IDEs: Choose an Editor for Your WorkflowGolang Interfaces: What They Are and How to Use ThemGoroutine Leaks in Go: How to Detect and Prevent ThemGoroutines in Go: Concurrency Without LeaksHow to Download and Install Golang SafelyHow to Learn Golang with a Focused Study PlanIntegration Testing in Go: Testing Real BoundariesJSON in Go: Encoding, Decoding, and ValidationLearn Golang with a Practical Beginner CourseMocking in Go: Test Doubles Without Magicsync.WaitGroup in Go: Coordinating Multiple GoroutinesTable-Driven Tests in Go: The Idiomatic WayTesting in Go: Table Tests, HTTP, Fakes, and CoverageWhat Is Go and How Does a Go Program Work?Why Learn Golang and What Is Go Used For?Writing Unit Tests in Go: A Practical Guide
Open full search and filters →

Golang Hello World and Your First Go Program

Build a Golang Hello World program, understand package main and func main, accept an argument, then run and compile it with Go 1.24.7.

Standard-library lessonGo requirement: Go 1.22+How tutorials are checked
Fundamentals · Lesson 1Saved in this browser. No account required.

Verification (10 August 2026): Self-contained runnable examples were compile-tested with Go 1.26.5 on Windows/amd64. Multi-file or contextual snippets and intentional compiler-error demonstrations were checked separately in the repository verification matrix.

Write and run your first Go program, then extend it to accept a name from the command line. You will understand package main, func main, imports, arguments, formatted strings, go run, and the boundary between reusable logic and the executable entry point.

Works with Go 1.22 and later. The complete example was executed with Go 1.24.7 on Windows amd64.

A Go executable starts in package main

An executable Go program uses package main and defines a main function. The go command compiles the package, then the operating system starts the resulting program at that entry point.

The complete Go tutorial places this first program in the wider language path. If Go is not installed yet, follow download and install Go first.

Build a useful Hello World

This version keeps greeting behavior in a function. That small separation makes the decision testable without starting another process.

package main

import (
	"fmt"
	"os"
)

func greeting(args []string) string {
	if len(args) == 0 {
		return "Hello, Gopher!"
	}
	return fmt.Sprintf("Hello, %s!", args[0])
}

func main() {
	fmt.Println(greeting(os.Args[1:]))
}

Run it with a name:

go run main.go Gopher

Output observed with Go 1.24.7:

Hello, Gopher!

os.Args contains the executable name at index zero. Passing os.Args[1:] gives greeting only the user-supplied arguments. The empty-slice branch prevents an out-of-range panic when no name is supplied.

Understand every line

package main

Files in the same directory normally declare the same package. The special name main tells the toolchain this package builds an executable rather than an importable library.

Imports

fmt supplies printing and formatted strings. os exposes process information, including command-line arguments. Go rejects unused imports, which keeps dependencies visible and deliberate.

func main()

The runtime calls main after package initialization. It takes no arguments and returns no value. Use return values and errors in ordinary functions, then let main decide what to print or which exit code to use.

Turn the first program into a small command

Extend the program in three steps:

  1. Accept --shout and uppercase the greeting.
  2. Move option parsing into a function returning a value and an error.
  3. Add table-driven tests for no name, one name, and invalid arguments.

When the command grows beyond one file, read Golang file structure before creating packages. When you want an executable file rather than a temporary run, use Go compilation and execution.

Common mistakes

Running from the wrong directory

If the terminal cannot find main.go, change into the directory containing the file or run the package path explicitly.

Naming the package something other than main

A package intended to build a command needs package main and a main function. Importable packages should use descriptive package names and must not contain the command entry point.

Indexing arguments without checking length

This unsafe assumption panics when the command receives no name:

name := os.Args[1] // broken when len(os.Args) < 2

Check the length or pass os.Args[1:] into a function that handles empty input.

What next

Learn the difference between go run, go build, and go install in Go compilation and execution. Then follow the beginner course and use the study guide to turn this command into a tested project.