百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
G

go-interface-examples

> 编程语言
开源

go-interface-examples 使用 Go 接口编写更清晰、更易于测试的代码的一些示例。这些示例假定对 Go 语言有一定的了解。

346 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

go-interface-examples 使用 Go 接口编写更清晰、更易于测试的代码的一些示例。这些示例假定对 Go 语言有一定的了解。

go-interface-examples

Some examples of using Go interfaces to make cleaner, more testable code.

These assume general familiarity with Go syntax and the ability to write simple programs. If you haven't already, take a tour of Go! You'll be introduced to interfaces there. You should also read Effective Go.

The point of this repository is to expand on those ideas and explore them with simple but relatable examples to explain why Go tells you to use interfaces the way it does.

The Outside World

What are interfaces? What problems do interfaces solve? Why do I care?

My code is hard to test because it has to talk to a database! What do? What's this 'mock' thing I hear about?

Why should I pass in dependencies instead of just creating them or using globals?

Let's find out!

This is pretty entry level. We'll explore creating a simple service that ends up being easy to reason about and easy to test thanks to Go interfaces. We'll start as simple as possible and explore why we make each iteration of changes towards a more robust solution. This will be "interfaces the long way".

I think this is a good read if you're new to Go and/or relatively new to coding in general. You may find it useful even if you're intermediate, however, as I hope it articulates a lot of the underlying reasons behind doing the things you may already be doing with interfaces/IoC/DI.

Declare interfaces locally

Why does Go tell me to declare interfaces at the consumer level?

What's up with this tweet?

See the article here.

There's a reason libraries don't usually give you an interface to work with, and why you shouldn't provide one either. Let's explore why.

This is an example of defining your interfaces locally in each package rather than trying to do an "interface package" or providing giant interfaces from a single package that also contains the implementation.

I think this is a good read if you're coming to Go from another language and may have preconceived ideas of what interfaces should be because of that. You'll need to be at least a little familiar with Go at this point and may have written a service or two.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Go

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言