<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: nicolas ggd</title>
    <description>The latest articles on Forem by nicolas ggd (@nicolas_ggd_5a565e9c7ad59).</description>
    <link>https://forem.com/nicolas_ggd_5a565e9c7ad59</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3804731%2F892fece4-6e45-487f-8df5-33e91acd4c90.jpg</url>
      <title>Forem: nicolas ggd</title>
      <link>https://forem.com/nicolas_ggd_5a565e9c7ad59</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nicolas_ggd_5a565e9c7ad59"/>
    <language>en</language>
    <item>
      <title>How I Used go/ast to Auto-Generate API Docs from Go HTTP Handlers</title>
      <dc:creator>nicolas ggd</dc:creator>
      <pubDate>Tue, 03 Mar 2026 22:46:02 +0000</pubDate>
      <link>https://forem.com/nicolas_ggd_5a565e9c7ad59/how-i-used-goast-to-auto-generate-api-docs-from-go-http-handlers-54km</link>
      <guid>https://forem.com/nicolas_ggd_5a565e9c7ad59/how-i-used-goast-to-auto-generate-api-docs-from-go-http-handlers-54km</guid>
      <description>&lt;p&gt;Most Go API documentation tools require you to litter your code with annotations. I wanted to see how far static analysis could go — extracting routes, parameters, request/response types, and even auth patterns purely from the AST. Turns out, pretty far.&lt;/p&gt;

&lt;p&gt;This is the story of building &lt;a href="https://github.com/syst3mctl/godoclive" rel="noopener noreferrer"&gt;GoDoc Live&lt;/a&gt;, an open-source CLI that reads your Go HTTP handlers and generates interactive documentation without touching your code.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;If you've used swaggo, you know the drill: dozens of comment annotations above every handler. They drift out of sync with the actual code. You end up maintaining two sources of truth.&lt;/p&gt;

&lt;p&gt;I wanted a tool where the Go source code IS the documentation. Point it at your project, get docs.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Architecture
&lt;/h2&gt;

&lt;p&gt;The analysis pipeline has 8 stages:&lt;/p&gt;

&lt;h3&gt;
  
  
  Stage 1-2: Load &amp;amp; Detect
&lt;/h3&gt;

&lt;p&gt;Everything starts with &lt;code&gt;go/packages&lt;/code&gt; in &lt;code&gt;LoadAllSyntax&lt;/code&gt; mode. This gives us both the full AST and type information for every package. Then we walk import declarations to detect whether the project uses chi, gin, or net/http stdlib.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stage 3-4: Extract Routes &amp;amp; Resolve Handlers
&lt;/h3&gt;

&lt;p&gt;This is where it gets interesting. We walk the AST of &lt;code&gt;main()&lt;/code&gt; and &lt;code&gt;init()&lt;/code&gt; functions, looking for router method calls like &lt;code&gt;.Get()&lt;/code&gt;, &lt;code&gt;.Post()&lt;/code&gt;, &lt;code&gt;.Group()&lt;/code&gt;, and &lt;code&gt;.Mount()&lt;/code&gt;. For net/http stdlib, we detect the new Go 1.22+ &lt;code&gt;"METHOD /path"&lt;/code&gt; patterns in &lt;code&gt;http.HandleFunc&lt;/code&gt; and &lt;code&gt;mux.Handle&lt;/code&gt; calls.&lt;/p&gt;

&lt;p&gt;Each route registration references a handler — sometimes a function literal, sometimes an identifier, sometimes a selector expression like &lt;code&gt;handlers.GetUser&lt;/code&gt;. We resolve each one back to its &lt;code&gt;*ast.FuncDecl&lt;/code&gt; to analyze the actual handler body.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stage 5: Contract Extraction (The Hard Part)
&lt;/h3&gt;

&lt;p&gt;This is where most of the complexity lives. For each handler, we extract:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Path parameters:&lt;/strong&gt; Name heuristics (&lt;code&gt;{id}&lt;/code&gt; → uuid, &lt;code&gt;{page}&lt;/code&gt; → integer) combined with handler body analysis to see how the param is actually used after extraction&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Query parameters:&lt;/strong&gt; Pattern-matching on &lt;code&gt;r.URL.Query().Get()&lt;/code&gt; and gin's &lt;code&gt;c.DefaultQuery()&lt;/code&gt; to detect required vs. optional and default values&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Request bodies:&lt;/strong&gt; Matching &lt;code&gt;json.NewDecoder(r.Body).Decode(&amp;amp;target)&lt;/code&gt; and &lt;code&gt;c.ShouldBindJSON(&amp;amp;target)&lt;/code&gt;, then resolving &lt;code&gt;target&lt;/code&gt; through the type checker to get full struct definitions with JSON tags&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Responses:&lt;/strong&gt; Branch-aware analysis that pairs each &lt;code&gt;WriteHeader()&lt;/code&gt; or &lt;code&gt;c.JSON()&lt;/code&gt; call with the response body type in that branch's scope&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Stage 6: Type Mapping
&lt;/h3&gt;

&lt;p&gt;We convert &lt;code&gt;types.Type&lt;/code&gt; into a recursive &lt;code&gt;TypeDef&lt;/code&gt; structure. This handles nested structs, slices, maps, pointer types — all with their JSON tags, field names, and example values. The type checker from &lt;code&gt;go/types&lt;/code&gt; does the heavy lifting here, resolving identifiers across packages.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stage 7: Auth Detection
&lt;/h3&gt;

&lt;p&gt;We scan middleware function bodies for patterns like &lt;code&gt;r.Header.Get("Authorization")&lt;/code&gt;, JWT-related function calls, and API key extraction. This identifies whether endpoints are protected and what scheme they use.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stage 8: Generate
&lt;/h3&gt;

&lt;p&gt;Finally, all endpoint contracts get transformed into an interactive HTML documentation site with a dark/light theme, search, and a "Try It" panel.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Hardest Problems
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Branch-aware response extraction:&lt;/strong&gt; A single handler might return 200 with a user object, 404 with an error, or 500 with a different error — all from different &lt;code&gt;if/else&lt;/code&gt; branches. Walking the AST tree and tracking which response type belongs to which status code required careful scope tracking.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Helper function tracing:&lt;/strong&gt; Real-world Go code rarely calls &lt;code&gt;w.WriteHeader()&lt;/code&gt; directly. Instead, developers use helpers like &lt;code&gt;respond(w, 200, data)&lt;/code&gt; or &lt;code&gt;writeJSON(w, result)&lt;/code&gt;. GoDoc Live traces one level deep — when it encounters an unknown function call in a handler, it resolves and analyzes that function's body to find the actual HTTP response patterns.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;net/http stdlib detection:&lt;/strong&gt; Go 1.22 introduced &lt;code&gt;"GET /users/{id}"&lt;/code&gt; patterns in &lt;code&gt;http.HandleFunc&lt;/code&gt;. Detecting these requires parsing the string literal in the first argument of the call expression and extracting both the method and path pattern — a different approach than chi/gin where methods are encoded in the function name.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I Learned
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;go/ast&lt;/code&gt; combined with &lt;code&gt;go/types&lt;/code&gt; is remarkably powerful. The type checker handles the hard parts — resolving identifiers across packages, interface satisfaction, and concrete types from expressions. The real challenge wasn't parsing Go code; it was handling the sheer variety of patterns developers use to write HTTP handlers.&lt;/p&gt;

&lt;p&gt;The tool currently hits 100% accuracy across 42 test endpoints for route detection, path params, query params, response status codes, and auth detection. The main limitation is single-level helper tracing — deeply nested abstractions will produce incomplete contracts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try It
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;go &lt;span class="nb"&gt;install &lt;/span&gt;github.com/syst3mctl/godoclive/cmd/godoclive@latest
godoclive generate ./...
open docs/index.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or with live reload:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;godoclive watch &lt;span class="nt"&gt;--serve&lt;/span&gt; :8080 ./...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The project is MIT licensed: &lt;a href="https://github.com/syst3mctl/godoclive" rel="noopener noreferrer"&gt;github.com/syst3mctl/godoclive&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'd love to hear about edge cases in handler patterns I haven't considered, or if anyone has tackled similar static analysis problems in Go.&lt;/p&gt;

</description>
      <category>go</category>
      <category>opensource</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
