Get Started in Three Steps
Go from zero to running a real web server with routing in under 5 minutes.Success! You’ve just run your first Go web server with routing.
Understanding the Code
Let’s break down what you just ran. The Intro project demonstrates Go’s standard project layout.Project Structure
Why this structure?
cmd/contains application entry points (main packages)internal/contains private application code that can’t be imported by external projects- This is the Go community’s preferred layout for production applications
The Entry Point
Thecmd/intro/main.go file is where your application starts:
cmd/intro/main.go
Code Breakdown
Code Breakdown
package main: Tells Go this is an executable program (not a library)routes.NewRouter(): Gets a router from our internal packagehttp.ListenAndServe(): Starts the HTTP server on port 8080- Error handling: Go returns errors as values, not exceptions
The Router Logic
Theinternal/routes/routes.go file handles HTTP routing:
internal/routes/routes.go
How Routing Works
How Routing Works
http.ServeMux: Go’s built-in HTTP request routerHandleFunc: Registers a handler function for a specific URL pattern- Handler signature: All handlers take
(w http.ResponseWriter, r *http.Request) - Response writing: Use
fmt.Fprintln(w, ...)to send data back to the client
What You Just Learned
Go Modules
Go Modules
The This enables dependency management and allows other projects to import your code.
go.mod file defines your module path and Go version:Package Organization
Package Organization
- Every folder in Go is a package
- All files in a folder must declare the same package name
package maincreates an executable- Other package names create libraries
Import Paths
Import Paths
When you import code, you use the full module path:Go’s toolchain enforces that
internal/ packages can’t be imported by external modules.HTTP Handlers
HTTP Handlers
Go’s standard library includes a production-ready HTTP server:
- No external dependencies required
- Fast and concurrent by default (each request in its own goroutine)
- Simple handler interface: just a function taking
ResponseWriterandRequest
Try These Experiments
Now that you have a working server, try making some changes:Add a New Route
Open Then create the handler:Restart the server and visit
internal/routes/routes.go and add:http://localhost:8080/helloReturn JSON
Add JSON support to your API:
Change the Port
In Now your server runs on port 3000.
cmd/intro/main.go, change:Build a Binary
Compile your code into an executable:You now have a standalone binary with no dependencies!
Next Steps
Now that you have Go running, it’s time to dive deeper:Project Structure
Understand why we organize code with
cmd/ and internal/ directoriesThe Go Runtime
Learn how Go manages memory, garbage collection, and escape analysis
Methods & Receivers
Master the difference between value and pointer receivers
Build a REST API
Create a full CRUD API with JSON, routing, and database integration
Common Issues
Port Already in Use
Port Already in Use
If you see
bind: address already in use, another process is using port 8080.Solution: Either kill that process or change the port in main.go:Go Command Not Found
Go Command Not Found
If Add this to your
go version doesn’t work, Go isn’t in your PATH.Solution: Add Go to your PATH (usually done automatically by the installer):~/.bashrc or ~/.zshrc to make it permanent.Module Not Found
Module Not Found
If you see module import errors, make sure you’re in the correct directory:
Hot Reload Not Working
Hot Reload Not Working
Go doesn’t have built-in hot reload. You must restart after changes.Solution: Use a tool like air for automatic reloading:
Need Help? Join the Go community on r/golang, Gophers Slack, or check out the official Go documentation.