Project Structure
Start with Go Project Structure
A practical Go project layout starts small and grows when package boundaries become useful.
What you’ll learn
- Explain the main purpose of go project structure.
- Read and run the complete Go example in this lesson.
- Apply the concept and recognize common mistakes.
Understand the idea
For a tiny command-line program, go.mod and main.go are sufficient. Larger repositories commonly place executable entry points in cmd/, share public package code in appropriate directories, and reserve internal/ for packages that Go intentionally restricts to eligible importers.
No single directory layout is mandatory. Design around responsibilities and the number of programs, rather than copying a large template on day one. Tests conventionally live alongside the code they test as _test.go files.
Visualize how it works
Follow this diagram before executing the example. Check where a value or decision changes what runs next.
Tools, syntax and practical notes
Example as a project grows
myapp/
go.mod
cmd/
server/
main.go
internal/
config/
config.go
README.mdThe layout is an option, not a Go requirement. More details: Organizing a Go module.
Run a complete Go example
This example is a complete program. Save it as main.go or paste it into the Go Playground.
Go code
package main
import "fmt"
type Config struct{ AppName string }
func printBanner(c Config) { fmt.Println("App:", c.AppName) }
func main() { printBanner(Config{AppName: "go-tools"}) }
Output
App: go-toolsTrace what the program does
The program separates configuration data and a printing function without splitting it into unnecessary packages. If the project grows, those responsibilities can be moved into new directories.
Avoid common mistakes
- Go does not require
src/inside every module. - internal/ is a special import-visibility convention, not merely a descriptive folder name.
Try it yourself
Sketch a two-command repository using cmd/server and cmd/worker, plus an internal/config package.
Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.
Check your understanding
What you should remember
You have practiced Go Project Structure in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.
Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.
Reaching the recap and completing the quizzes are separate milestones.