{"id":13615751,"url":"https://github.com/go-kiss/monkey","last_synced_at":"2025-07-09T19:33:21.496Z","repository":{"id":43277140,"uuid":"405527049","full_name":"go-kiss/monkey","owner":"go-kiss","description":"Go语言猴子补丁框架","archived":false,"fork":false,"pushed_at":"2024-05-08T03:06:02.000Z","size":57,"stargazers_count":129,"open_issues_count":2,"forks_count":18,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-05T06:41:18.752Z","etag":null,"topics":["golang","monkey-patching"],"latest_commit_sha":null,"homepage":"https://taoshu.in/go/monkey/","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/go-kiss.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-09-12T02:23:26.000Z","updated_at":"2025-03-05T09:08:44.000Z","dependencies_parsed_at":"2024-06-18T18:35:15.073Z","dependency_job_id":"3b771a70-f1f1-4f59-968e-daa8965e40a0","html_url":"https://github.com/go-kiss/monkey","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/go-kiss/monkey","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-kiss%2Fmonkey","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-kiss%2Fmonkey/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-kiss%2Fmonkey/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-kiss%2Fmonkey/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/go-kiss","download_url":"https://codeload.github.com/go-kiss/monkey/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/go-kiss%2Fmonkey/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264502490,"owners_count":23618617,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","monkey-patching"],"created_at":"2024-08-01T20:01:17.434Z","updated_at":"2025-07-09T19:33:21.242Z","avatar_url":"https://github.com/go-kiss.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# Go语言猴子补丁框架 🙉 🐒\n\n![test workflow](https://github.com/go-kiss/monkey/actions/workflows/go.yml/badge.svg)\n\nGo 语言猴子补丁（monkey patching）框架。\n\n本项目对 [Bouke](https://bou.ke/blog/monkey-patching-in-go/) 的项目做了优化，不同协程可以独立 patch 同一个函数而互不影响。从而可以并发运行单元测试。\n\n工作原理请参考我的系列文章：\n\n- [Go语言实现猴子补丁](https://taoshu.in/go/monkey.html)\n- [Go语言实现猴子补丁【二】](https://taoshu.in/go/monkey-2.html)\n- [Go语言实现猴子补丁【三】](https://taoshu.in/go/monkey-3.html)\n\nBouke 已经不再维护原项目，所以只能开一个新项目了🤣。\n\n## 快速入门\n\n首先，引入 monkey 包\n\n```bash\ngo get github.com/go-kiss/monkey\n```\n\n然后，调用 `monkey.Patch` 方法 mock 指定函数。\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/go-kiss/monkey\"\n)\n\nfunc sum(a, b int) int { return a + b }\n\nfunc sub1[T int|float64](a, b T) T { return a - b }\nfunc sub2[T int|float64](a, b T) T { return a + b }\n\ntype S struct { i int }\n\nfunc (s *S) Get() int { return i }\n\nfunc main() {\n\t// mock 普通函数\n\tmonkey.Patch(sum, func(a b int) int { return a - b })\n\tfmt.Println(sum(1, 2)) // 输出 -1\n\t// mock 泛型函数\n\tmonkey.Patch(sub1[int], sub2[int])\n\tfmt.Println(sub1(1, 2)) // 输出 3\n\t// mock 结构体方法\n\tmonkey.Patch((*s).Get, func(s *S) int { return -1 })\n\tvar s S\n\tfmt.Println(s.Get()) // 输出 -1\n}\n```\n\n更多用法请参考[使用示例](./examples)和[测试用例](./monkey_test.go)。\n\n## 注意事项\n\n1. Monkey 需要关闭 Go 语言的内联优化才能生效，比如测试的时候需要：`go test -gcflags='all=-N -l'`。\n2. Monkey 需要在运行的时候修改内存代码段，因而无法在一些对安全性要求比较高的系统上工作。\n3. Monkey 不应该用于生产系统，但用来 mock 测试代码还是没有问题的。\n4. Monkey 目前仅支持 amd64 指令架构，支持 linux/macos/windows 平台。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-kiss%2Fmonkey","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgo-kiss%2Fmonkey","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgo-kiss%2Fmonkey/lists"}