Hello World & Workspace
In this lesson, you'll write your first Go program, understand the structure of a Go application, and learn how to use Go modules for dependency management. By the end, you'll know how to create, run, and build Go programs.
Your First Go Program
Let's start with the classic "Hello, World!" program. This simple program demonstrates the basic structure of every Go application.
Click Run to execute your code
Understanding the Code
Let's break down each part of this program:
package main- Declares this file belongs to the main package. The main package is specialโit defines an executable program.import "fmt"- Imports the fmt (format) package from the standard library for formatted I/O.func main()- The main function is the entry point of the program. Every executable Go program must have a main function in the main package.fmt.Println()- Prints text to the console with a newline.
{ must be on the
same line as
the function declaration. This is enforced by the language syntax!
Running Go Programs
Go provides two main ways to execute your code:
1. go run (Quick Execution)
The go run command compiles and runs your program in one step. It's
perfect for
development and testing:
# Run the program directly
go run hello-world.go
# Output: Hello, World!
go run compiles your code to a
temporary executable
and runs it immediately. The executable is deleted after the program finishes.
2. go build (Create Executable)
The go build command creates a standalone executable file:
# Build an executable
go build hello-world.go
# This creates an executable named 'hello-world' (or 'hello-world.exe' on Windows)
# Run the executable
./hello-world
# Output: Hello, World!
go run during development for
quick testing,
and go build when you need to distribute your program or measure
performance.
Go Modules
Go modules are the standard way to manage dependencies in Go projects. A module is a collection of related Go packages that are versioned together.
Creating a Module
To create a new Go module, use the go mod init command:
# Create a new directory for your project
mkdir myproject
cd myproject
# Initialize a new module
go mod init example.com/myproject
# This creates a go.mod file
The go.mod file tracks your module's dependencies:
module example.com/myproject
go 1.21
example.com/myproject) is
typically a URL where your code could be found. For local projects, you can use
any name, but
it's good practice to use a domain-style path.
Module Demo
Here's a program that demonstrates basic module concepts:
Click Run to execute your code
Go Project Structure
A typical Go project follows this structure:
myproject/
โโโ go.mod # Module definition
โโโ go.sum # Dependency checksums (auto-generated)
โโโ main.go # Main application
โโโ utils/ # Package directory
โ โโโ helper.go # Package file
โโโ README.md # Documentation
- Each directory is a package
- Package name should match directory name (except for main)
- Files in the same directory must have the same package declaration
- The
mainpackage can be in any directory
Package vs Module
Understanding the difference between packages and modules is important:
| Concept | Description | Example |
|---|---|---|
| Package | A collection of Go files in the same directory | package main, package fmt |
| Module | A collection of packages versioned together | module example.com/myapp |
| Import Path | How you reference a package | import "fmt" |
Essential Go Commands
Here are the most important Go commands you'll use:
| Command | Purpose | Example |
|---|---|---|
go run |
Compile and run | go run main.go |
go build |
Compile to executable | go build |
go mod init |
Initialize module | go mod init myapp |
go mod tidy |
Clean up dependencies | go mod tidy |
go fmt |
Format code | go fmt ./... |
go version |
Check Go version | go version |
Common Mistakes
1. Wrong package name
Executable programs must use package main:
// โ Wrong - won't create executable
package myapp
func main() {
// ...
}
// โ
Correct
package main
func main() {
// ...
}
2. Missing main function
The main package must have a main function:
// โ Wrong - no entry point
package main
func start() {
fmt.Println("Hello")
}
// โ
Correct
package main
func main() {
fmt.Println("Hello")
}
3. Incorrect brace placement
Opening braces must be on the same line:
// โ Wrong - syntax error
func main()
{
fmt.Println("Hello")
}
// โ
Correct
func main() {
fmt.Println("Hello")
}
Exercise: Create Your First Project
Task: Create a Go program that prints information about yourself.
Requirements:
- Create a new directory called
myinfo - Initialize it as a Go module
- Create a
main.gofile - Print your name, age, and favorite hobby
- Use
fmt.Printffor formatted output
Steps:
# 1. Create and enter directory
mkdir myinfo
cd myinfo
# 2. Initialize module
go mod init myinfo
# 3. Create main.go with your code
# 4. Run it
go run main.go
Show Solution
package main
import "fmt"
func main() {
name := "Alice"
age := 25
hobby := "coding in Go"
fmt.Printf("Hi! I'm %s\n", name)
fmt.Printf("I'm %d years old\n", age)
fmt.Printf("I love %s!\n", hobby)
// Alternative: using Println
fmt.Println("\n--- About Me ---")
fmt.Println("Name:", name)
fmt.Println("Age:", age)
fmt.Println("Hobby:", hobby)
}
Summary
- Every Go program starts with a package declaration
- Executable programs use
package mainandfunc main() - go run compiles and runs code in one step (development)
- go build creates a standalone executable (distribution)
- Go modules manage dependencies with
go.mod - go mod init initializes a new module
- Opening braces must be on the same line in Go
- fmt package provides formatted I/O functions
What's Next?
Now that you can create and run Go programs, it's time to learn about Variables & Types. In the next lesson, you'll discover how to store and manipulate data in Go, including Go's type system and variable declarations.
Enjoying these tutorials?