How to Organize a Go Project

Standard Project Layout

Use cmd for apps, internal for private code, and pkg for public libraries in your Go project.

Organize your Go project by placing source code in cmd/ for executables, internal/ for private libraries, and pkg/ for public libraries, with a go.mod file at the root.

myproject/
  go.mod
  cmd/
    myapp/
      main.go
  internal/
    config/
      config.go
  pkg/
    utils/
      utils.go
  1. Initialize the module with go mod init example.com/myproject.
  2. Create the cmd/myapp directory and add main.go to define your entry point.
  3. Move shared private code into internal/config to prevent external imports.
  4. Move public library code into pkg/utils for external consumption.