Web Analytics

Hello World & Workspace

Beginner ~30 min read

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.

Output
Click Run to execute your code

Understanding the Code

Let's break down each part of this program:

Code Breakdown:
  • 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.
Pro Tip: In Go, the opening brace { 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!
How it works: 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!
Best Practice: Use 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
Module Path: The module path (e.g., 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:

Output
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
Key Points:
  • 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 main package 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:

  1. Create a new directory called myinfo
  2. Initialize it as a Go module
  3. Create a main.go file
  4. Print your name, age, and favorite hobby
  5. Use fmt.Printf for 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 main and func 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.