Structure Go projects using a layered approach with cmd for entry points, internal for private logic, and pkg for public libraries.
Use a layered structure (cmd, internal, pkg) to balance simplicity and maintainability for most Go projects. Place your main entry point in cmd/yourapp/main.go, shared logic in internal/, and reusable libraries in pkg/.
// cmd/yourapp/main.go
package main
import (
"yourmodule/internal/service"
)
func main() {
service.Run()
}
// internal/service/service.go
package service
func Run() {
// Business logic here
}
Think of your project like a house: the cmd folder is the front door where the program starts, internal is the private living room where your specific logic lives, and pkg is the public garage for tools others might borrow. This keeps your code organized and prevents outside projects from accidentally breaking your private logic.