Introduction to Go
Learn what Go is, set up your development environment, and write your first Go program
Learning Objectives
- Understand what Go is and why it's popular
- Install Go on your computer
- Write and run your first Go program
- Use the Go Playground for quick experimentation
What is Go?
Go (often called Golang) is an open-source programming language developed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. First released in 2009, Go was designed to address common criticisms of other languages while maintaining their positive characteristics.
Why Learn Go?
Go has become incredibly popular for building modern software. Here’s why:
Who Uses Go?
| Company | Use Case |
|---|---|
| Infrastructure, cloud services | |
| Docker | Container runtime |
| Kubernetes | Container orchestration |
| Uber | High-performance services |
| Twitch | Video streaming backend |
| Dropbox | Performance-critical systems |
Go’s Key Features
| Feature | Description |
|---|---|
| Compiled | Code compiles to native machine code |
| Statically Typed | Types are checked at compile time |
| Garbage Collected | Automatic memory management |
| Concurrent | Built-in goroutines and channels |
| Simple | Only 25 keywords in the language |
| Fast Compilation | Compiles in seconds, not minutes |
Installing Go
Let’s get Go installed on your computer.
Step 1: Download Go
- Visit go.dev/dl
- Download the installer for your operating system
- Run the installer and follow the prompts
Step 2: Verify Installation
Open your terminal (Command Prompt on Windows, Terminal on macOS/Linux) and run:
|
|
You should see output like:
go version go1.21.0 darwin/amd64
Step 3: Check Your Environment
Run this command to see your Go environment:
|
|
- GOROOT: Where Go is installed
- GOPATH: Your workspace for Go projects (usually
~/go)
Your First Go Program
Let’s write the classic “Hello, World!” program.
Using the Go Playground
The fastest way to try Go is the Go Playground - an online editor that runs Go code in your browser.
Visit the playground and enter this code:
|
|
Click “Run” and you’ll see:
Hello, World!
Creating a Local Project
Let’s create a proper Go project on your computer.
Step 1: Create a project directory
|
|
Step 2: Initialize a Go module
|
|
This creates a go.mod file that tracks your project’s dependencies.
Step 3: Create the main file
Create a file named main.go with the following content:
|
|
Step 4: Run your program
|
|
Output:
Hello, World!
Welcome to Go in 30 Days!
Understanding the Code
Let’s break down what we wrote:
Package Declaration
Every Go file starts with a package declaration:
|
|
package mainis special - it tells Go this is an executable program- Other packages (like
fmt) are libraries that provide functionality
Import Statement
|
|
importbrings in code from other packagesfmtis the formatting package for input/output- You can import multiple packages:
|
|
The main Function
|
|
funcdeclares a functionmainis the entry point - Go starts here{}contains the function body
Printing Output
|
|
fmt.Printlnprints a line of text- Text in quotes is a string
Printlnadds a newline at the end
Go Commands
Go comes with powerful built-in tools:
| Command | Description |
|---|---|
go run |
Compile and run a program |
go build |
Compile a program into an executable |
go fmt |
Format your code automatically |
go mod init |
Initialize a new module |
go mod tidy |
Clean up module dependencies |
go test |
Run tests |
Building an Executable
Instead of go run, you can build a standalone executable:
|
|
This creates an executable file called hello (or hello.exe on Windows) that you can run directly:
|
|
Go can compile for different operating systems! Set GOOS and GOARCH to build for other platforms:
|
|
Code Formatting with go fmt
Go has a standard code formatter. Run it on your code:
|
|
This automatically formats your code to Go’s standard style. No more debates about tabs vs spaces!
Summary
Today you learned:
- Go is a simple, fast, and concurrent programming language
- How to install Go and verify your installation
- How to write and run your first Go program
- The structure of a Go program (package, import, main)
- Essential Go commands (
go run,go build,go fmt)
Practice Exercise
Before moving on, try these exercises:
- Modify the hello program to print your name
- Add a third
fmt.Printlnstatement - Try using
fmt.Printinstead offmt.Println- what’s different? - Build an executable and run it
Next Steps
Tomorrow, we’ll dive into variables and data types - the foundation of every Go program!
Now, complete the quiz below to test your knowledge and unlock Day 2!
Quiz
Complete this quiz with a minimum score of 80% to mark Day 1 as complete.
Discussion
Have questions or want to discuss this lesson? Join the conversation below!