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

ifacemaker

> 编程语言
开源

从结构方法生成接口.

354 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

从结构方法生成接口.

ifacemaker

This is a development helper program that generates a Golang interface by inspecting the structure methods of an existing .go file. The primary use case is to generate interfaces for gomock, so that gomock can generate mocks from those interfaces. This makes unit testing easier.

The tool understands Go generics and will correctly generate interfaces for generic types and methods.

Install

bash
go install github.com/vburenin/ifacemaker@latest

Usage

Here is the help output of ifacemaker:

…

As an example, let's say you wanted to generate an interface for the Human structure in this sample code:

go
package main

import "fmt"

type Human struct {
 name string
 age  int
}

// Returns the name of our Human.
func (h *Human) GetName() string {
 return h.name
}

// Our Human just had a birthday! Increase its age.
func (h *Human) Birthday() {
 h.age += 1
 fmt.Printf("I am now %d years old!\n", h.age)
}

// Make the Human say hello.
func (h *Human) SayHello() {
 fmt.Printf("Hello, my name is %s, and I am %d years old.\n", h.name, h.age)
}

func main() {
 human := &Human{name: "Bob", age: 30}
 human.GetName()
 human.SayHello()
 human.Birthday()
}

The ifacemaker helper program can generate this interface for you:

bash
$ ifacemaker -f human.go -s Human -i HumanIface -p humantest -y "HumanIface makes human interaction easy" -c "DONT EDIT: Auto generated"
// DONT EDIT: Auto generated

package humantest

// HumanIface makes human interaction easy
type HumanIface interface {
 // Returns the name of our Human.
 GetName() string
 // Our Human just had a birthday! Increase its age.
 Birthday()
 // Make the Human say hello.
 SayHello()
}

$

In the above example, ifacemaker inspected the structure methods of the Human struct and generated an interface called HumanIface in the humantest package. Note that the ifacemaker program preserves docstrings by default.

You can tell ifacemaker to write its output to a file, versus stdout, using the -o parameter:

bash
$ ifacemaker -f human.go -s Human -i HumanIface -p humantest -y "HumanIface makes human interaction easy" -c "DONT EDIT: Auto generated" -o humaniface.go
$

You can also run it with Docker:

bash
$ docker run --rm -v $(pwd):/tmp/ vburenin/ifacemaker -f /tmp/human.go -s Human -i HumanIface -p humantest -y "HumanIface makes human interaction easy" -c "DONT EDIT: Auto generated" -o /tmp/humaniface.go
$

Issues· 0 开放

查看全部 Issues在 GitHub 打开

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

> 标签

Go

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

> 工具信息

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

> 相关工具

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