# Golang Interface 内部实现

DevFeed: [Golang Interface 内部实现](<https://devfeed.tech/articles/golang-interface-40985.md>)

Original publisher: [Read original article](<https://blog.joway.io/posts/golang-interface-internal/>)

Author: Joway

Published: 2021-01-20T00:00:00Z

Content type: tutorial

Language: zh

Sources: [Random Thoughts](<https://devfeed.tech/sources/random-thoughts.md>)

Topics: [interface](<https://devfeed.tech/topics/interface.md>)

Tags: [fmt](<https://devfeed.tech/tags/fmt.md>), [implement](<https://devfeed.tech/tags/implement.md>), [interface](<https://devfeed.tech/tags/interface.md>), [run](<https://devfeed.tech/tags/run.md>), [tech](<https://devfeed.tech/tags/tech.md>), [type](<https://devfeed.tech/tags/type.md>)

## AI overview

The article explains how Golang interface values are represented internally and how a non-atomic assignment can leave type information present while the stored pointer is nil. This can make a nil check fail, allow a type conversion to succeed, and cause a panic only when the method is called.

## Source excerpt

最近遇到一个由于 Golang Interface 底层实现，引发的线上 panic 问题，虽然根源在于赋值操作没有保护起来，却意外地发现了关于 interface 的一些有意思的底层细节。 假设我们现在有以下定义： type Interface interface { Run() } type Implement struct { n int } func (i *Implement) Run() { fmt.Printf(i.n) } 对于使用者而言，一个变量无论是 Interface 类型或是 *Implement 类型，差别都不大。