{"id":38865082,"url":"https://github.com/ggq89/mutualdep","last_synced_at":"2026-01-17T14:24:50.098Z","repository":{"id":57509070,"uuid":"144434981","full_name":"ggq89/mutualdep","owner":"ggq89","description":"Dealing with the mutual dependencies in Golang","archived":false,"fork":false,"pushed_at":"2019-07-17T05:50:15.000Z","size":11,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-15T03:20:40.469Z","etag":null,"topics":["cycle","dependencies","golang","import","mutual"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ggq89.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-12T03:32:08.000Z","updated_at":"2020-05-21T09:09:25.000Z","dependencies_parsed_at":"2022-08-30T07:10:11.284Z","dependency_job_id":null,"html_url":"https://github.com/ggq89/mutualdep","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ggq89/mutualdep","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ggq89%2Fmutualdep","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ggq89%2Fmutualdep/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ggq89%2Fmutualdep/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ggq89%2Fmutualdep/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ggq89","download_url":"https://codeload.github.com/ggq89/mutualdep/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ggq89%2Fmutualdep/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28509946,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-17T13:38:16.342Z","status":"ssl_error","status_checked_at":"2026-01-17T13:37:44.060Z","response_time":85,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["cycle","dependencies","golang","import","mutual"],"created_at":"2026-01-17T14:24:50.037Z","updated_at":"2026-01-17T14:24:50.089Z","avatar_url":"https://github.com/ggq89.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mutualdep\n\nHandling mutual dependencies in Golang\n\n本文同时发布个人CSDN博客： https://blog.csdn.net/ggq89/article/details/81148558\n\n相信不少 `Gopher` 在写 `Golang` 程序都遇到过 `import cycle not allowed` 问题，本人最近研读 [go-ethereum](https://github.com/ethereum/go-ethereum) 源码时，发现定义 `interface` 也能解决此问题， 还能解决连分包都不能解决的情况， 并且比分包更加简单快捷。下面逐个讲解 `分包` 和 `定义接口` 这两种方法。\n\n# 目录： #\n\n* [1. 应用场景](#1-应用场景)\n* [2. 代码实现](#2-代码实现)\n* [3. 定义接口](#3-定义接口)\n* [4. 拆分包](#4-拆分包)\n* [5. 总结](#5-总结)\n\n# 1. 应用场景\n\n假设有如下使用场景：\n\n1. `A` 是应用程序的框架级结构体，在 `A` 包含子模块 `B` 和 `C` 的指针；\n2. `B` 为了方便的使用应用的其他子模块（比如 `C` ）功能，所以在其结构体包含了 `A` 的指针；\n3. `C` 要调用 `A` 包中的某个方法；\n\n# 2. 代码实现\n\n其程序大致如下：\n\n* `package a` 代码如下：\n\n```golang\npackage a\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/ggq89/mutualdep/b\"\n\t\"github.com/ggq89/mutualdep/c\"\n)\n\ntype A struct {\n\tPb *b.B\n\tPc *c.C\n}\n\nfunc New(ic int) *A {\n\ta := \u0026A{\n\t\tPc: c.New(ic),\n\t}\n\n\ta.Pb = b.New(a)\n\n\treturn a\n}\n\nfunc Printf(v int) {\n\tfmt.Printf(\"%v\", v)\n}\n```\n\n* `package b` 代码如下：\n\n```go\npackage b\n\nimport (\n\t\"github.com/ggq89/mutualdep/a\"\n)\n\ntype B struct {\n\tPa *a.A\n}\n\nfunc New(a *a.A) *B {\n\treturn \u0026B{\n\t\tPa: a,\n\t}\n}\n\nfunc (b *B) DisplayC() {\n\tb.Pa.Pc.Show()\n}\n```\n\n* `package c` 代码如下：\n\n```go\npackage c\n\nimport \"github.com/ggq89/mutualdep/a\"\n\ntype C struct {\n\tVc int\n}\n\nfunc New(i int) *C {\n\treturn \u0026C{\n\t\tVc: i,\n\t}\n}\n\nfunc (c *C) Show() {\n\ta.Printf(c.Vc)\n}\n```\n\n`package a` 依赖 `package b` 和 `package c`，同时 `package b` 依赖 `package a` 、 `package c` 也依赖 `package a` 。\n\n`main` 函数代码如下：\n\n```go\npackage main\n\nimport \"github.com/ggq89/mutualdep/a\"\n\nfunc main() {\n\ta := a.New(3)\n\ta.Pb.DisplayC()\n}\n```\n\n编译时就会报错如下：\n\n```bash\nimport cycle not allowed\npackage main\n\timports github.com/ggq89/mutualdep/a\n\timports github.com/ggq89/mutualdep/b\n\timports github.com/ggq89/mutualdep/a\n```\n\n# 3. 定义接口\n\n现在的问题是：\n\n```bash\nA depends on B\nB depends on A\n```\n\n对于 `A struct` 和 `B struct` 有彼此的指针这种相互依赖问题，可以使用定义接口的方法解决，具体步骤如下：\n\n* 在 `package b` 中 定义 `a interface ` ； 将 `b` 所有使用到结构体 `a` 的变量和方法的地方全部转化成 使用接口 `a` 的方法；在 `a interface ` 中补充缺少的方法；\n\n经过上面的步骤处理后， `package b` 代码如下：\n\n```go\npackage b\n\nimport (\n\t\"github.com/ggq89/mutualdep/c\"\n)\n\ntype B struct {\n\tPa a\n}\n\ntype a interface {\n\tGetC() *c.C\n}\n\nfunc New(a a) *B {\n\treturn \u0026B{\n\t\tPa:a,\n\t}\n}\n\nfunc (b *B) DisplayC() {\n\tb.Pa.GetC().Show()\n}\n```\n\n* 在 `package a` 中补充可能缺少的方法；\n\n处理后， `package a` 中的代码如下：\n\n```go\npackage a\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/ggq89/mutualdep/b\"\n\t\"github.com/ggq89/mutualdep/c\"\n)\n\ntype A struct {\n\tPb *b.B\n\tPc *c.C\n}\n\nfunc New(ic int) *A {\n\ta := \u0026A{\n\t\tPc:c.New(ic),\n\t}\n\n\ta.Pb = b.New(a)\n\n\treturn a\n}\n\nfunc (a *A)GetC() *c.C {\n\treturn a.Pc\n}\n\nfunc Printf(v int)  {\n\tfmt.Printf(\"%v\", v)\n}\n```\n\n# 4. 拆分包\n\n再次编译，提示如下：\n\n```\nimport cycle not allowed\npackage main\n\timports github.com/ggq89/mutualdep/a\n\timports github.com/ggq89/mutualdep/b\n\timports github.com/ggq89/mutualdep/c\n\timports github.com/ggq89/mutualdep/a\n```\n\n现在是另一个相互依赖问题：\n\n```bash\nA depends on C\nC depends on A\n```\n\n与前面的相互依赖不同，前面的依赖是由于 `A struct` 和 `B struct` 有彼此的指针导致的，属于硬相互依赖；\n\n而这里是由于 `package c` 中的方法调用 `package a` 中的方法引起的，属于软相互依赖；\n\n* 这种相互依赖可以通过将方法拆分到另一个包的方式来解决；在拆分包的过程中，可能会将结构体的方法转化为普通的函数；\n\n引入 `package f` ， 将方法迁移到 `f` 中 :\n\n```go\npackage f\n\nimport \"fmt\"\n\nfunc Printf(v int) {\n\tfmt.Printf(\"%v\", v)\n}\n```\n\n方法移动到 `package f` 后， `package a` 的代码如下：\n\n```go\npackage a\n\nimport (\n\t\"github.com/ggq89/mutualdep/b\"\n\t\"github.com/ggq89/mutualdep/c\"\n)\n\ntype A struct {\n\tPb *b.B\n\tPc *c.C\n}\n\nfunc New(ic int) *A {\n\ta := \u0026A{\n\t\tPc: c.New(ic),\n\t}\n\n\ta.Pb = b.New(a)\n\n\treturn a\n}\n\nfunc (a *A) GetC() *c.C {\n\treturn a.Pc\n}\n```\n\n`package c`随之改成调用`package f`，其代码如下：\n\n```go\npackage c\n\nimport (\n\t\"github.com/ggq89/mutualdep/a/f\"\n)\n\ntype C struct {\n\tVc int\n}\n\nfunc New(i int) *C {\n\treturn \u0026C{\n\t\tVc: i,\n\t}\n}\n\nfunc (c *C) Show() {\n\tf.Printf(c.Vc)\n}\n```\n\n现在依赖关系如下：\n\n```bash\nA depends on B and C\nB depends on C\nC depends on F\n```\n\n至此，两种包相互依赖关系都得以解决。\n\n# 5. 总结\n\n1. 对于软相互依赖，利用分包的方法就能解决，有些函数导致的相互依赖只能通过分包解决；分包能细化包的功能；\n\n2. 对于硬相互依赖只能通过定义接口的方法解决；定义接口能提高包的独立性，同时也提高了追踪代码调用关系的难度；\n\n---\n\n参考文章：\n\n* golang不允许循环import问题(\"import cycle not allowed\") : http://ju.outofmemory.cn/entry/230115\n* golang解决import cycle not allowed的一种思路 : https://studygolang.com/articles/10582?fr=sidebar\n* golang中(\"import cycle not allowed\")错误 : https://blog.csdn.net/skh2015java/article/details/53943784\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fggq89%2Fmutualdep","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fggq89%2Fmutualdep","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fggq89%2Fmutualdep/lists"}