{"id":434,"date":"2026-05-27T09:00:00","date_gmt":"2026-05-27T03:30:00","guid":{"rendered":"https:\/\/smitabharat.com\/blog\/go-clean-architecture\/"},"modified":"2026-05-27T09:00:00","modified_gmt":"2026-05-27T03:30:00","slug":"go-clean-architecture","status":"publish","type":"post","link":"https:\/\/smitabharat.com\/blog\/go-clean-architecture\/","title":{"rendered":"Go Clean Architecture: Structure a Real Project"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Go clean architecture is a way to organise your code. It keeps business rules free from databases, web frameworks and other tools. In this post, you will learn the layers, the dependency rule and a simple folder layout. Then we will build a small book service, where you can swap the storage without touching the business code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This lesson builds on <a href=\"https:\/\/smitabharat.com\/blog\/go-interfaces-examples\/\">Go interfaces with examples<\/a> and <a href=\"https:\/\/smitabharat.com\/blog\/go-modules-packages-explained\/\">Go modules and packages<\/a>. We also reuse ideas from <a href=\"https:\/\/smitabharat.com\/blog\/go-mocking-interfaces-testing\/\">Go mocking interfaces for testing<\/a> and <a href=\"https:\/\/smitabharat.com\/blog\/go-rest-api-tutorial\/\">Go REST API for beginners<\/a>. Because interfaces are the key tool here, read that post first if needed. Also, a quick look at the structs lesson helps.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Is Go Clean Architecture?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In short, Go clean architecture splits a program into layers. The inner layer holds your business rules, such as &#8220;a book needs a title and an author&#8221;. Meanwhile, the outer layers hold details, such as HTTP handlers and database code. The most important rule is simple. Outer layers may depend on inner layers, but never the other way round.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Think of a power plug. Your laptop does not care whether the electricity comes from solar panels or a coal plant. It only needs a standard socket. Similarly, your business code should only need a standard interface. It should not care if the data lives in memory, PostgreSQL or a file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Go suits Go clean architecture well, because interfaces are small and satisfied implicitly. As a result, you can define an interface in the inner layer. Then an outer package can implement it without importing anything special.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Go Clean Architecture Matters<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Small programs can live in one file, and that is fine. However, real services grow, and tangled code becomes slow to change. For example, a new storage engine could force edits in twenty places. So here is what Go clean architecture gives you.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Easy testing<\/strong>: the business layer needs no database or server in its tests.<\/li>\n\n\n\n<li><strong>Swappable tools<\/strong>: you can change the database or the web layer without a rewrite.<\/li>\n\n\n\n<li><strong>Clear ownership<\/strong>: every package has one job, so new teammates find code quickly.<\/li>\n\n\n\n<li><strong>Safer changes<\/strong>: a change in the HTTP layer cannot break business rules.<\/li>\n\n\n\n<li><strong>Longer life<\/strong>: the core of the app outlives any single library.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Because of this, teams adopt clean architecture when a project passes a few thousand lines. In fact, many Go teams use it as a shared language for code reviews. Therefore, new teammates understand the layout on day one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How Go Clean Architecture Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In Go clean architecture, you can think of three rings. First, in the centre sits the domain and service code, which holds entities and use cases. Next, around it sit the adapters, which turn the outside world into calls the service understands. Finally, at the edge sit the frameworks and drivers, such as <code>net\/http<\/code> and PostgreSQL. Calls flow inward, and the dependency arrows point inward too.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-clean-architecture-diagram-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-clean-architecture-diagram-1-1024x640.png\" alt=\"Go Clean Architecture diagram showing the HTTP and storage adapters depending on the book service\" class=\"wp-image-421\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-clean-architecture-diagram-1-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-clean-architecture-diagram-1-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-clean-architecture-diagram-1-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-clean-architecture-diagram-1-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-clean-architecture-diagram-1-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Three layers: the HTTP handler and the repository sit outside, and both depend on the service in the centre. The service only knows an interface, never the database.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Notice the clever part. The service needs to save books, but it does not know how. So it defines a <code>Repository<\/code> interface, and the database package implements it. As a result, the usual dependency flips. In other words, the database depends on the business code, not the reverse.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Layers and Their Jobs<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Each package has one clear job, so the table below lists the layers used in this lesson.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Layer<\/th><th>Package<\/th><th>Job<\/th><\/tr><\/thead><tbody><tr><td>Domain and service<\/td><td><code>internal\/book<\/code><\/td><td>Entities, rules and the <code>Repository<\/code> interface<\/td><\/tr><tr><td>Storage adapter<\/td><td><code>internal\/memory<\/code><\/td><td>Implements <code>Repository<\/code> with a map<\/td><\/tr><tr><td>HTTP adapter<\/td><td><code>internal\/httpapi<\/code><\/td><td>Turns requests into service calls<\/td><\/tr><tr><td>Entry point<\/td><td><code>cmd\/api<\/code><\/td><td>Wires every part together<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Also, the <code>internal<\/code> folder is special in Go. Only code in the same module can import it. As a result, outsiders cannot depend on your private parts.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Folder Layout<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the layout that we build. Every file has a single purpose.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bookstore\/\n  go.mod\n  cmd\/api\/main.go\n  internal\/book\/book.go\n  internal\/book\/service_test.go\n  internal\/memory\/repo.go\n  internal\/httpapi\/handler.go<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Go Clean Architecture vs a Flat Layout<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Many beginners put everything in <code>main.go<\/code>. That works for small tools, but it gets hard later. So the table below compares it with a layered design.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Flat layout<\/th><th>Clean layout<\/th><\/tr><\/thead><tbody><tr><td>Files<\/td><td>One or two<\/td><td>Several small packages<\/td><\/tr><tr><td>Testing the rules<\/td><td>Needs HTTP and storage<\/td><td>Needs only a fake repository<\/td><\/tr><tr><td>Changing the database<\/td><td>Edits spread everywhere<\/td><td>One new adapter<\/td><\/tr><tr><td>Time to start<\/td><td>Minutes<\/td><td>A little longer<\/td><\/tr><tr><td>Best for<\/td><td>Scripts and prototypes<\/td><td>Services that will grow<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><a href=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-clean-architecture-diagram-2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-clean-architecture-diagram-2-1024x640.png\" alt=\"Go Clean Architecture diagram comparing a flat layout with a clean layout\" class=\"wp-image-422\" srcset=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-clean-architecture-diagram-2-1024x640.png 1024w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-clean-architecture-diagram-2-300x188.png 300w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-clean-architecture-diagram-2-768x480.png 768w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-clean-architecture-diagram-2-1536x960.png 1536w, https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-clean-architecture-diagram-2-2048x1280.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">A flat layout mixes HTTP, rules and storage in one file, while a clean layout keeps each concern in its own package.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">So when should you use which? Use a flat layout for scripts and early prototypes. On the other hand, move to the clean layout when a second developer joins. You should also move when the service needs real tests.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Go Clean Architecture Hands-On: A Book Service<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s build the service. Because we use only the standard library, you can run it anywhere. Also, every output below comes from a real run on Go 1.24.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: The Domain and Service<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">First, <code>internal\/book\/book.go<\/code> holds the entity, the errors, the interface and the use cases. It imports nothing from outside the standard library.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package book\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"strings\"\n)\n\nvar (\n\tErrNotFound = errors.New(\"book not found\")\n\tErrInvalid  = errors.New(\"invalid book\")\n)\n\ntype Book struct {\n\tID     int\n\tTitle  string\n\tAuthor string\n}\n\n\/\/ Repository is the port. The service only knows this interface.\ntype Repository interface {\n\tSave(ctx context.Context, b Book) (Book, error)\n\tFind(ctx context.Context, id int) (Book, error)\n}\n\ntype Service struct {\n\trepo Repository\n}\n\nfunc NewService(repo Repository) *Service { return &amp;Service{repo: repo} }\n\nfunc (s *Service) Add(ctx context.Context, title, author string) (Book, error) {\n\ttitle, author = strings.TrimSpace(title), strings.TrimSpace(author)\n\tif title == \"\" || author == \"\" {\n\t\treturn Book{}, ErrInvalid\n\t}\n\treturn s.repo.Save(ctx, Book{Title: title, Author: author})\n}\n\nfunc (s *Service) Get(ctx context.Context, id int) (Book, error) {\n\treturn s.repo.Find(ctx, id)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Notice that the service takes the interface in its constructor. This is called dependency injection. However, it is only a fancy name for passing what you need as an argument.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: The Storage Adapter<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Next, <code>internal\/memory\/repo.go<\/code> implements the interface with a map and a mutex. Later, you can write a PostgreSQL version with the same methods.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package memory\n\nimport (\n\t\"context\"\n\t\"sync\"\n\n\t\"bookstore\/internal\/book\"\n)\n\ntype Repo struct {\n\tmu     sync.Mutex\n\tnextID int\n\tbooks  map[int]book.Book\n}\n\nfunc New() *Repo { return &amp;Repo{nextID: 1, books: map[int]book.Book{}} }\n\nfunc (r *Repo) Save(_ context.Context, b book.Book) (book.Book, error) {\n\tr.mu.Lock()\n\tdefer r.mu.Unlock()\n\tb.ID = r.nextID\n\tr.nextID++\n\tr.books[b.ID] = b\n\treturn b, nil\n}\n\nfunc (r *Repo) Find(_ context.Context, id int) (book.Book, error) {\n\tr.mu.Lock()\n\tdefer r.mu.Unlock()\n\tb, ok := r.books[id]\n\tif !ok {\n\t\treturn book.Book{}, book.ErrNotFound\n\t}\n\treturn b, nil\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: The HTTP Adapter<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Then <code>internal\/httpapi\/handler.go<\/code> turns requests into service calls. It also maps business errors to status codes in one place. First, here is the top of the file, with the constructor and the create route.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package httpapi\n\nimport (\n\t\"encoding\/json\"\n\t\"errors\"\n\t\"net\/http\"\n\t\"strconv\"\n\n\t\"bookstore\/internal\/book\"\n)\n\ntype Handler struct {\n\tsvc *book.Service\n}\n\nfunc New(svc *book.Service) http.Handler {\n\th := &amp;Handler{svc: svc}\n\tmux := http.NewServeMux()\n\tmux.HandleFunc(\"POST \/books\", h.create)\n\tmux.HandleFunc(\"GET \/books\/{id}\", h.get)\n\treturn mux\n}\n\nfunc (h *Handler) create(w http.ResponseWriter, r *http.Request) {\n\tvar in struct {\n\t\tTitle  string `json:\"title\"`\n\t\tAuthor string `json:\"author\"`\n\t}\n\tif err := json.NewDecoder(r.Body).Decode(&amp;in); err != nil {\n\t\thttp.Error(w, \"invalid JSON\", http.StatusBadRequest)\n\t\treturn\n\t}\n\tb, err := h.svc.Add(r.Context(), in.Title, in.Author)\n\tif err != nil {\n\t\twriteError(w, err)\n\t\treturn\n\t}\n\tw.WriteHeader(http.StatusCreated)\n\tjson.NewEncoder(w).Encode(b)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next, here is the read route and the error mapping.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func (h *Handler) get(w http.ResponseWriter, r *http.Request) {\n\tid, err := strconv.Atoi(r.PathValue(\"id\"))\n\tif err != nil {\n\t\thttp.Error(w, \"invalid id\", http.StatusBadRequest)\n\t\treturn\n\t}\n\tb, err := h.svc.Get(r.Context(), id)\n\tif err != nil {\n\t\twriteError(w, err)\n\t\treturn\n\t}\n\tjson.NewEncoder(w).Encode(b)\n}\n\n\/\/ writeError maps business errors to HTTP status codes.\nfunc writeError(w http.ResponseWriter, err error) {\n\tswitch {\n\tcase errors.Is(err, book.ErrNotFound):\n\t\thttp.Error(w, err.Error(), http.StatusNotFound)\n\tcase errors.Is(err, book.ErrInvalid):\n\t\thttp.Error(w, err.Error(), http.StatusUnprocessableEntity)\n\tdefault:\n\t\thttp.Error(w, \"internal error\", http.StatusInternalServerError)\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The handler knows about HTTP and about the service. However, it knows nothing about how books are stored. That separation is the whole point of Go clean architecture.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Wire Everything in main<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, <code>cmd\/api\/main.go<\/code> creates each part and connects them. It is the only place that knows every package.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"fmt\"\n\t\"net\/http\/httptest\"\n\t\"strings\"\n\n\t\"bookstore\/internal\/book\"\n\t\"bookstore\/internal\/httpapi\"\n\t\"bookstore\/internal\/memory\"\n)\n\nfunc main() {\n\trepo := memory.New()\n\tsvc := book.NewService(repo)\n\thandler := httpapi.New(svc)\n\n\t\/\/ In a real app: http.ListenAndServe(\":8080\", handler)\n\tdo := func(method, path, body string) {\n\t\treq := httptest.NewRequest(method, path, strings.NewReader(body))\n\t\trec := httptest.NewRecorder()\n\t\thandler.ServeHTTP(rec, req)\n\t\tfmt.Printf(\"%s %s -&gt; %d %s\", method, path, rec.Code, rec.Body.String())\n\t}\n\tdo(\"POST\", \"\/books\", `{\"title\":\"Go in Action\",\"author\":\"William Kennedy\"}`)\n\tdo(\"GET\", \"\/books\/1\", \"\")\n\tdo(\"GET\", \"\/books\/9\", \"\")\n\tdo(\"POST\", \"\/books\", `{\"title\":\"\",\"author\":\"Nobody\"}`)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now run <code>go run .\/cmd\/api<\/code>. As a result, you see the real output below. The business rules work, and each error maps to the right status code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>POST \/books -&gt; 201 {\"ID\":1,\"Title\":\"Go in Action\",\"Author\":\"William Kennedy\"}\nGET \/books\/1 -&gt; 200 {\"ID\":1,\"Title\":\"Go in Action\",\"Author\":\"William Kennedy\"}\nGET \/books\/9 -&gt; 404 book not found\nPOST \/books -&gt; 422 invalid book<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Test the Rules with a Fake<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Because the service depends on an interface, a test can pass a tiny fake. Therefore, no database or server is needed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package book\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"testing\"\n)\n\ntype fakeRepo struct{ saved []Book }\n\nfunc (f *fakeRepo) Save(_ context.Context, b Book) (Book, error) {\n\tb.ID = len(f.saved) + 1\n\tf.saved = append(f.saved, b)\n\treturn b, nil\n}\n\nfunc (f *fakeRepo) Find(_ context.Context, id int) (Book, error) {\n\treturn Book{}, ErrNotFound\n}\n\nfunc TestAddRejectsEmptyTitle(t *testing.T) {\n\trepo := &amp;fakeRepo{}\n\tsvc := NewService(repo)\n\n\t_, err := svc.Add(context.Background(), \"   \", \"Someone\")\n\tif !errors.Is(err, ErrInvalid) {\n\t\tt.Fatalf(\"err = %v, want ErrInvalid\", err)\n\t}\n\tif len(repo.saved) != 0 {\n\t\tt.Errorf(\"saved %d books, want 0\", len(repo.saved))\n\t}\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Running <code>go test .\/...<\/code> gives <code>ok  bookstore\/internal\/book<\/code>. The test proves that an empty title is rejected. It also proves that nothing reaches storage. Also, it runs in milliseconds, because no real database is involved.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common Mistakes with Go Clean Architecture<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Too many layers too early<\/strong>: tiny apps do not need five packages. So start small, and split when it hurts.<\/li>\n\n\n\n<li><strong>Interfaces on the wrong side<\/strong>: if the storage package defines the interface, the service must import it. Instead, define it where it is used.<\/li>\n\n\n\n<li><strong>Leaking HTTP types inward<\/strong>: passing <code>*http.Request<\/code> into the service ties it to the web. So pass plain values and a context.<\/li>\n\n\n\n<li><strong>One giant interface<\/strong>: big interfaces are hard to fake. Therefore, keep them small.<\/li>\n\n\n\n<li><strong>Business rules in handlers<\/strong>: rules then repeat in every adapter. Instead, keep them in the service, so every adapter gets the same behaviour for free.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices for Go Clean Architecture<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Define interfaces in the package that uses them<\/strong>, because it keeps dependencies pointing inward.<\/li>\n\n\n\n<li><strong>Return domain errors<\/strong>, such as <code>ErrNotFound<\/code>, and let adapters map them to status codes.<\/li>\n\n\n\n<li>**Use the <code>internal<\/code> folder**, since it blocks outside imports. As a result, other modules cannot depend on your private code by accident.<\/li>\n\n\n\n<li>**Wire everything in <code>main<\/code>**, which keeps the rest of the code free from setup. Therefore, the other packages stay easy to reuse.<\/li>\n\n\n\n<li>**Pass <code>context.Context<\/code> first**, because it supports timeouts and cancellation.<\/li>\n\n\n\n<li><strong>Test the service with fakes<\/strong>, and test adapters with <code>httptest<\/code> or a real database. This split keeps the fast tests fast.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Practice Task<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To practise Go clean architecture, add a <code>Delete<\/code> method to the service. First, add it to the <code>Repository<\/code> interface. Then implement it in the memory adapter, add a <code>DELETE \/books\/{id}<\/code> route and write a test with a fake.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hint: return <code>ErrNotFound<\/code> when the id is missing. After that, map it to 404 in <code>writeError<\/code>. Finally, try writing a second adapter that stores books in a JSON file, and swap it in <code>main<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">These pages cover the language features and the project layout advice used here.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/go.dev\/doc\/modules\/layout\">Organizing a Go module<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/go.dev\/wiki\/CodeReviewComments#interfaces\">Go Wiki: CodeReviewComments on interfaces<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/go.dev\/doc\/effective_go#interfaces_and_types\">Effective Go: interfaces<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Go clean architecture keeps business rules free from databases and web code.<\/li>\n\n\n\n<li>Outer layers depend on inner layers, never the other way round.<\/li>\n\n\n\n<li>The service defines the interface, and the adapter implements it.<\/li>\n\n\n\n<li>Dependency injection means passing what you need as an argument.<\/li>\n\n\n\n<li>The <code>internal<\/code> folder and <code>main<\/code> wiring keep the design tidy.<\/li>\n\n\n\n<li>Fakes make business tests fast, because they need no real storage.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By now, you can structure a service cleanly. However, many libraries rely on a hidden Go feature to read your structs. So in the next lesson, we will look under the hood at the <code>reflect<\/code> package and see how libraries read struct tags.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next up: <a href=\"https:\/\/smitabharat.com\/blog\/go-reflection-basics\/\">Go reflection basics<\/a><\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Go clean architecture is a way to organise your code. It keeps business rules free from databases, web frameworks and other tools. In this&hellip;<\/p>\n","protected":false},"author":1,"featured_media":420,"comment_status":"","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[15],"tags":[80,83,11],"swapz_series":[],"class_list":["post-434","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go-lang","tag-advanced","tag-go-architecture","tag-go-lang"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Go Clean Architecture: Structure a Real Project<\/title>\n<meta name=\"description\" content=\"Learn Go clean architecture step by step: split layers, define interfaces where they are used, inject dependencies and test with fakes.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/smitabharat.com\/blog\/go-clean-architecture\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Go Clean Architecture: Structure a Real Project\" \/>\n<meta property=\"og:description\" content=\"Learn Go clean architecture step by step: split layers, define interfaces where they are used, inject dependencies and test with fakes.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/smitabharat.com\/blog\/go-clean-architecture\/\" \/>\n<meta property=\"og:site_name\" content=\"Smita Bharat - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-05-27T03:30:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-clean-architecture-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Smita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Smita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-clean-architecture\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-clean-architecture\\\/\"},\"author\":{\"name\":\"Smita\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"headline\":\"Go Clean Architecture: Structure a Real Project\",\"datePublished\":\"2026-05-27T03:30:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-clean-architecture\\\/\"},\"wordCount\":1506,\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-clean-architecture\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-clean-architecture-featured.png\",\"keywords\":[\"Advanced\",\"Go Architecture\",\"Go Lang\"],\"articleSection\":[\"Go Lang\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-clean-architecture\\\/\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-clean-architecture\\\/\",\"name\":\"Go Clean Architecture: Structure a Real Project\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-clean-architecture\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-clean-architecture\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-clean-architecture-featured.png\",\"datePublished\":\"2026-05-27T03:30:00+00:00\",\"description\":\"Learn Go clean architecture step by step: split layers, define interfaces where they are used, inject dependencies and test with fakes.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-clean-architecture\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-clean-architecture\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-clean-architecture\\\/#primaryimage\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-clean-architecture-featured.png\",\"contentUrl\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/10\\\/go-clean-architecture-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Go Clean Architecture featured image: business rules, HTTP adapter and storage adapter\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/go-clean-architecture\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Go Clean Architecture: Structure a Real Project\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/\",\"name\":\"Smita Bharat Blog\",\"description\":\"Golang &amp; Backend Development Tutorials and Interview Questions\",\"publisher\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\"},\"alternateName\":\"Smita Bharat\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/27a1b1067d396703215c54acedbfd339\",\"name\":\"Smita\",\"logo\":{\"@id\":\"https:\\\/\\\/smitabharat.com\\\/blog\\\/#\\\/schema\\\/person\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/smitabharat.com\\\/blog\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Go Clean Architecture: Structure a Real Project","description":"Learn Go clean architecture step by step: split layers, define interfaces where they are used, inject dependencies and test with fakes.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/smitabharat.com\/blog\/go-clean-architecture\/","og_locale":"en_US","og_type":"article","og_title":"Go Clean Architecture: Structure a Real Project","og_description":"Learn Go clean architecture step by step: split layers, define interfaces where they are used, inject dependencies and test with fakes.","og_url":"https:\/\/smitabharat.com\/blog\/go-clean-architecture\/","og_site_name":"Smita Bharat - Blog","article_published_time":"2026-05-27T03:30:00+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-clean-architecture-featured.png","type":"image\/png"}],"author":"Smita","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Smita","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/smitabharat.com\/blog\/go-clean-architecture\/#article","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/go-clean-architecture\/"},"author":{"name":"Smita","@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"headline":"Go Clean Architecture: Structure a Real Project","datePublished":"2026-05-27T03:30:00+00:00","mainEntityOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-clean-architecture\/"},"wordCount":1506,"publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-clean-architecture\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-clean-architecture-featured.png","keywords":["Advanced","Go Architecture","Go Lang"],"articleSection":["Go Lang"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/smitabharat.com\/blog\/go-clean-architecture\/","url":"https:\/\/smitabharat.com\/blog\/go-clean-architecture\/","name":"Go Clean Architecture: Structure a Real Project","isPartOf":{"@id":"https:\/\/smitabharat.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/smitabharat.com\/blog\/go-clean-architecture\/#primaryimage"},"image":{"@id":"https:\/\/smitabharat.com\/blog\/go-clean-architecture\/#primaryimage"},"thumbnailUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-clean-architecture-featured.png","datePublished":"2026-05-27T03:30:00+00:00","description":"Learn Go clean architecture step by step: split layers, define interfaces where they are used, inject dependencies and test with fakes.","breadcrumb":{"@id":"https:\/\/smitabharat.com\/blog\/go-clean-architecture\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/smitabharat.com\/blog\/go-clean-architecture\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/smitabharat.com\/blog\/go-clean-architecture\/#primaryimage","url":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-clean-architecture-featured.png","contentUrl":"https:\/\/smitabharat.com\/blog\/wp-content\/uploads\/2026\/10\/go-clean-architecture-featured.png","width":2400,"height":1260,"caption":"Go Clean Architecture featured image: business rules, HTTP adapter and storage adapter"},{"@type":"BreadcrumbList","@id":"https:\/\/smitabharat.com\/blog\/go-clean-architecture\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/smitabharat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Go Clean Architecture: Structure a Real Project"}]},{"@type":"WebSite","@id":"https:\/\/smitabharat.com\/blog\/#website","url":"https:\/\/smitabharat.com\/blog\/","name":"Smita Bharat Blog","description":"Golang &amp; Backend Development Tutorials and Interview Questions","publisher":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339"},"alternateName":"Smita Bharat","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/smitabharat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/27a1b1067d396703215c54acedbfd339","name":"Smita","logo":{"@id":"https:\/\/smitabharat.com\/blog\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/smitabharat.com\/blog"]}]}},"_links":{"self":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/comments?post=434"}],"version-history":[{"count":0,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/posts\/434\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media\/420"}],"wp:attachment":[{"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/media?parent=434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/categories?post=434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/tags?post=434"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/smitabharat.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}