---
title: "How Go Interfaces Work Under the Hood | jarmos.dev"
description: "Unpack the mechanics of Go interfaces. From memory structures to dynamic dispatch and runtime costs, learn how Golang interfaces work behind the scenes."
canonical_url: "https://jarmos.dev/drafts/how-go-interfaces-work-under-the-hood"
meta:
  "og:description": "Unpack the mechanics of Go interfaces. From memory structures to dynamic dispatch and runtime costs, learn how Golang interfaces work behind the scenes."
  "og:title": "How Go Interfaces Work Under the Hood | jarmos.dev"
---

> Index of every page on this site: [llms.txt](https://jarmos.dev/llms.txt)

Published on October 12, 2026 at 10:22 AM (last month)

# **How Go Interfaces Work Under the Hood**

Unpack the mechanics of Go interfaces. From memory structures to dynamic dispatch and runtime costs, learn how Golang interfaces work behind the scenes.

![How Go Interfaces Work Under the Hood]()

## 1. Introduction & The Implicit Interface Philosophy

- Implicit Satisfaction: Contrast Go's duck typing at compile-time with explicit `implements` interfaces in languages like Java or C#.
- The Core Thesis: While Go interfaces feel zero-cost and syntactic, they are concrete two-pointer structs managed dynamic-dispatch machinery at runtime.

## 2. Interface Internal Structures: eface vs. iface

- Explain how the Go runtime ( `src/runtime/runtime2.go`) categorizes `interfaces` into two distinct internal layouts:
- Empty Interface ( `eface`):
  - Used for `interface{}` / `any`.
  - Pointer to type metadata ( `_type`) and pointer to underlying data ( `data`).
- Non-Empty Interface ( `iface`):
  - Used for interfaces with methods (e.g., `io.Reader`).
  - Consists of an `itab` pointer ( `tab`) and `data` pointer ( `data`).

## 3. The `itab` Deep Dive (Interface Table)

- What is an `itab`? Explain that `itab` holds type information as well as the virtual function table (vtable).
- Anatomy of `itab`:
  - `inter`: Pointer to the interface type metadata.
  - `_type`: Pointer to the concrete type metadata.
  - `hash`: Copy of concrete type's hash for fast type assertions.
  - `fun`: Function pointer array mapping interface methods to concrete implementations.
  - `itab` Caching: How the runtime uses a global hash table to cache `itab` lookups so type conversions only pay the construction cost once.

## 4. The "Interface Equals Nil" Pitfall

- Explain why an `interface` containing a `nil` pointer is not a `nil` interface.
- Show how `iface.tab` exists while `iface.data` is `nil`, leading to subtle bugs:

  ```go
  var buf *bytes.Buffer // nil pointer var r io.Reader = buf // non-nil interface!
  (tab != nil, data == nil)

  fmt.Println(r == nil) // Evaluates to FALSE
  ```

## 5. Performance Costs & Escape Analysis

- Indirect Method Calls: Why dynamic dispatch via function pointers disables compiler inlining.
- Heap Allocations (Escape Analysis):
  - How converting values to interfaces often causes values to escape to the heap.
  - Pointer boxing: Primitive values or structs wrapped in `unsafe.Pointer`.
  - Benchmarks: Show a quick benchmark comparing direct method calls vs. interface dynamic calls.

## 6. Type Assertions & Type Switches Mechanics

- How `val.(T)` checks type matching using the `hash` field in `itab` or `_type`.
- The runtime function `assertE2I` / `assertI2I` mechanics.

## 7. Practical Engineering Takeaways

- Accept Interfaces, Return Structs: Why this idiom prevents unnecessary heap allocations and abstraction leaks.
- Keep Interfaces Small: Single-method interfaces (io.Reader, fmt.Stringer) maximize reuse and keep itab tables lean.
- Hot-Path Optimization: When to avoid interfaces in performance-critical inner loops to preserve inlining and zero-allocation guarantees.

## **Continue Reading... **

[**Thursday, 15 July 2021 at 12:00 am**<h3>**Vim or Neovim? Here is Why You Should Use the Latter!**</h3>![Vim vs Neovim: Which to Use?](https://ik.imagekit.io/jarmos/vim-vs-neovim.png?updatedAt=1702974992298)**Discover why Neovim excels over Vim! Faster, cleaner, and loaded with features like LSP and Lua scripting. Enjoy seamless transition for Vim users, faster performance with Lua, and user-friendly defaults. Neovim: the modern, minimalist text editor**** Read more **](https://jarmos.dev/blogs/vim-vs-neovim) [**Wednesday, 5 May 2021 at 12:00 am**<h3>**Using GitHub Actions to Deploy a FastAPI Project to Heroku**</h3>![Using GitHub Actions to Deploy a FastAPI Project to Heroku](https://ik.imagekit.io/jarmos/using-github-actions-to-deploy-to-heroku.png?updatedAt=1702974989351)**Deploy FastAPI projects to Heroku seamlessly using GitHub Actions. Avoid Heroku CLI and extra dependencies. Configure CI/CD pipeline with GitHub Actions, ensuring quality standards and last-line-of-defense health-checks. Detailed steps, potential roadblocks, and alternatives discussed.**** Read more **](https://jarmos.dev/blogs/using-github-actions-to-deploy-to-heroku) [**Wednesday, 13 May 2020 at 12:00 am**<h3>**Medium vs Static Site Generators: Which Should I Choose?**</h3>![Medium vs Static Site Generators](https://ik.imagekit.io/jarmos/medium-vs-static-site-generator.png?updatedAt=1702974989261)**Explore the challenges of building an online presence using Static Site Generators (SSGs) like Pelican, Hugo, and Gatsby. Delve into the principles of visual hierarchy for effective design. Discover the advantages of Medium over personal blogs in terms of accessibility, SEO, and audience reach. Understand the rationale behind choosing Medium and explore relevant references.**** Read more **](https://jarmos.dev/blogs/medium-vs-static-site-generators)

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://jarmos.dev/#website","@type":"WebSite","description":"I'm Jarmos - CTO at Weburz, Senior Engineer by title, open-source hacker by heart. I design systems, mentor devs, and occasionally tame misbehaving servers.","name":"jarmos.dev","url":"https://jarmos.dev/"},{"@id":"https://jarmos.dev/drafts/how-go-interfaces-work-under-the-hood#webpage","@type":"WebPage","description":"Unpack the mechanics of Go interfaces. From memory structures to dynamic dispatch and runtime costs, learn how Golang interfaces work behind the scenes.","name":"How Go Interfaces Work Under the Hood","url":"https://jarmos.dev/drafts/how-go-interfaces-work-under-the-hood","isPartOf":{"@id":"https://jarmos.dev/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://jarmos.dev/drafts/how-go-interfaces-work-under-the-hood"]}]}]}
```

## Sitemap

See the full [sitemap](https://jarmos.dev/sitemap.md) for all pages.
