Go Introduction
What Is Go (Golang)?
Go, also called Golang, is an open-source programming language designed for building fast, reliable, and scalable software. It compiles directly to machine code, runs on Windows, macOS, and Linux, and is used heavily for backend services, command-line tools, and cloud infrastructure.
Go was created at Google in 2007 by Robert Griesemer, Rob Pike, and Ken Thompson, and released publicly in 2009. The team wanted a language that kept the speed and safety of a compiled, statically typed language like C++, but with the simplicity and fast build times that large engineering teams needed — without the long compile cycles and complex tooling that C++ projects tend to accumulate at scale.
In practice, Go reads almost as simply as a scripting language, but it compiles to a single, self-contained binary and runs with the performance of a systems language.
Key Features of Go
- Statically typed — every variable has a fixed type known at compile time, so many mistakes are caught before the program ever runs.
- Compiled language — Go source code compiles directly to native machine code, producing fast, standalone executables with no external runtime required.
- Simple, minimal syntax — Go deliberately has a small set of keywords and language features, which makes it quick to learn and easy for teams to keep consistent.
- Built-in concurrency — goroutines and channels (covered in later lessons) let a Go program run many tasks at once without the complexity of traditional thread management.
- Automatic memory management — Go uses garbage collection, so you don't manually allocate and free memory as you would in C or C++.
- Cross-platform — the same source code can be compiled for Windows, macOS, Linux, and other platforms.
- Rich standard library — networking, file I/O, HTTP servers, JSON handling, and more ship with the language itself, with no extra installs needed for many common tasks.
Your First Go Program
Every Go program starts from a main function inside a main package. Here is the smallest complete Go program:
package main
import "fmt"
func main() {
message := "Welcome to Go Programming!"
fmt.Println(message)
}
Expected output:
Welcome to Go Programming!
What each line does:
package mainmarks this file as part of themainpackage. In Go, themainpackage is special — it tells the compiler this code produces a runnable program, not a reusable library.import "fmt"pulls in the standard library'sfmtpackage, which provides formatted input and output functions.func main()defines the entry point. When you run the compiled program, execution begins here.:=declares a new variable and infers its type from the assigned value — here,messagebecomes astring.fmt.Println(message)prints the value ofmessagefollowed by a newline.
What Is Go Used For?
Go's combination of speed, simplicity, and strong support for concurrent code has made it a common choice for infrastructure and backend engineering.
1. Web Development and APIs
Go's standard library includes an HTTP server, so you can build a working web service without any third-party framework:
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello from a Go web server!")
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
Running this program starts a web server on port 8080 that responds to every request with a greeting. This is the same pattern used to build production REST APIs, just with more routes and logic added.
2. Network Programming
Go's concurrency model — lightweight goroutines paired with channels — makes it well suited to servers that need to handle many simultaneous connections, such as chat servers or proxies:
package main
import (
"fmt"
"net"
)
func main() {
listener, err := net.Listen("tcp", ":9000")
if err != nil {
fmt.Println("Failed to start listener:", err)
return
}
fmt.Println("Server listening on port 9000")
for {
conn, err := listener.Accept()
if err != nil {
continue
}
go func(c net.Conn) {
c.Write([]byte("Connected to Go server\n"))
c.Close()
}(conn)
}
}
Each incoming connection is handed off to its own goroutine with go func(c net.Conn) { ... }(conn), so the server can accept new clients while it is still handling existing ones. (Concurrency and goroutines are covered in detail in a later lesson — for now, notice how little code is needed to handle multiple clients at once.)
Note: The original snippet ignored errors from
net.Listenandlistener.Accept()using the blank identifier_. In real code you should always check these errors, as shown above — a common mistake for beginners is discarding errors and being confused later when something silently fails.
3. Cloud-Native Development
Go is the language behind much of the modern cloud ecosystem — Docker, Kubernetes, Terraform, and Prometheus are all written in Go. Its fast startup time, small memory footprint, and easy cross-compilation make it a natural fit for microservices and command-line tools that get deployed inside containers.
4. Cross-Platform Applications
Because the Go compiler can target multiple operating systems and architectures from a single machine (a feature known as cross-compilation), you can build a Windows .exe, a macOS binary, and a Linux binary from the same source code without needing three separate machines.
Why Use Go?
- Easy to learn — a small, consistent syntax means less time spent memorizing language rules.
- High performance — as a compiled language, Go's runtime speed is close to C/C++ for most workloads.
- Fast compilation — Go was explicitly designed to compile quickly, even for large codebases, which keeps development cycles short.
- Built-in concurrency — goroutines make writing concurrent programs far simpler than working with raw OS threads.
- Automatic memory management — the garbage collector removes an entire category of manual memory bugs common in C/C++.
- Scalability — Go's simplicity and tooling scale well to large codebases and large engineering teams.
Go vs Python vs C++
| Feature | Go | Python | C++ |
|---|---|---|---|
| Typing | Statically typed | Dynamically typed | Statically typed |
| Execution | Compiled | Interpreted | Compiled |
| Runtime speed | Fast | Slower | Very fast |
| Compile time | Fast | No compilation step | Can be slow |
| Concurrency | Goroutines & channels | Limited (threading, GIL) | Threads |
| Garbage collection | Yes | Yes | No (manual memory management) |
| Object-oriented style | Composition via structs and interfaces (no classes or inheritance) | Full class-based OOP | Full class-based OOP |
| Inheritance | Not supported (uses composition instead) | Supported | Supported |
Common misconception: Go is often described as having "no OOP support" at all. That's not quite accurate — Go supports encapsulation and polymorphism through structs, methods, and interfaces; it simply has no
classkeyword and no classical inheritance. You'll see this style in the Structs lesson later in this series.