{"id":21610595,"url":"https://github.com/fishedee/gioc","last_synced_at":"2026-04-26T20:31:54.021Z","repository":{"id":79898781,"uuid":"110544343","full_name":"fishedee/gioc","owner":"fishedee","description":"awesome golang Inversion of Control(IOC) and Aspect Oriented Programming(aop)!","archived":false,"fork":false,"pushed_at":"2018-02-24T13:41:40.000Z","size":12,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-01T12:56:00.426Z","etag":null,"topics":["aop","aspect-oriented-programming","golang","ioc"],"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/fishedee.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2017-11-13T12:16:38.000Z","updated_at":"2018-04-26T05:33:22.000Z","dependencies_parsed_at":"2023-05-31T09:45:25.521Z","dependency_job_id":null,"html_url":"https://github.com/fishedee/gioc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/fishedee/gioc","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fishedee%2Fgioc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fishedee%2Fgioc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fishedee%2Fgioc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fishedee%2Fgioc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fishedee","download_url":"https://codeload.github.com/fishedee/gioc/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fishedee%2Fgioc/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32312231,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-26T19:15:34.056Z","status":"ssl_error","status_checked_at":"2026-04-26T19:15:15.467Z","response_time":129,"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":["aop","aspect-oriented-programming","golang","ioc"],"created_at":"2024-11-24T21:08:50.505Z","updated_at":"2026-04-26T20:31:54.016Z","avatar_url":"https://github.com/fishedee.png","language":"Go","readme":"# gioc\n\nawesome golang ioc and aop!Only Hava 3 function,Very Very Easy To Use!\n\n# Language\n\n* [English](https://github.com/fishedee/gioc/blob/master/README.md)\n* [简体中文](https://github.com/fishedee/gioc/blob/master/README_zh_cn.md)\n\n# IOC\n\n```go\ntype User struct {\n\tId   int\n\tName string\n}\ntype UserAo interface {\n\tGet(id int) User\n\tAdd(data User) int\n}\ntype UserDb interface {\n\tGet(id int) User\n\tAdd(data User) int\n}\n```\n\ndefine two bean interface\n\n```go\ntype userDbImpl struct {\n}\n\nfunc (this *userDbImpl) Get(id int) api.User {\n\treturn api.User{Name:\"Fish\"}\n}\n\nfunc (this *userDbImpl) Add(data api.User) int {\n\treturn 10001\n}\n\nfunc newUserDbImpl() api.UserDb {\n\treturn \u0026userDbImpl{}\n}\nfunc init() {\n\tgioc.Register(newUserDbImpl)\n}\n```\n\ndefine UserDb implment and register it by gioc.Register.You should notice that constructor function argument is rely and return value is target\n\n```go\ntype userAoImpl struct {\n\tuserDb api.UserDb\n}\n\nfunc (this *userAoImpl) Get(id int) api.User {\n\treturn this.userDb.Get(id)\n}\n\nfunc (this *userAoImpl) Add(data api.User) int {\n\treturn this.userDb.Add(data)\n}\n\nfunc newUserAoImpl(userDb api.UserDb) api.UserAo {\n\tuserAo := \u0026userAoImpl{}\n\tuserAo.userDb = userDb\n\treturn userAo\n}\n\nfunc init() {\n\tgioc.Register(newUserAoImpl)\n}\n```\n\ndefine UserAo implment and register it by gioc.Register.You should notice that UserAo need UserDb to construct\n\n```go\nuserAo := gioc.New((*api.UserAo)(nil), nil, nil).(api.UserAo)\n```\n\nuse gioc.New to get userAo.gioc will handle all rely,awesome!second argument and third argument is empty。\n\n# STUB\n\n```go\ntype UserDbStub struct {\n}\n\nfunc (this *UserDbStub) Get(id int) api.User {\n\treturn api.User{Id: 10001, Name: \"Fish\"}\n}\n\nfunc (this *UserDbStub) Add(data api.User) int {\n\treturn 10002\n}\n\nuserAo := gioc.New((*api.UserAo)(nil), []interface{}{\n\tfunc() api.UserDb {\n\t\treturn \u0026UserDbStub{}\n\t},\n}, nil).(api.UserAo)\n```\n\nuse gioc.New to get userAo,but second argument represent which bean is replace by the new bean,this trick is used to unit test fake mock and stub.\n\n# AOP\n\n```go\ntype UserAo interface {\n\tGet(id int) User\n\tAdd(data User) int\n}\n\ntype UserAoHook struct {\n\tGetHandler func(id int) User\n\tAddHandler func(data User) int\n}\n\nfunc (this *UserAoHook) Get(id int) User {\n\treturn this.GetHandler(id)\n}\n\nfunc (this *UserAoHook) Add(data User) int {\n\treturn this.AddHandler(data)\n}\n\nfunc NewUserAoHook() UserAo {\n\treturn \u0026UserAoHook{}\n}\n\nfunc init() {\n\tgioc.RegisterHook(NewUserAoHook)\n}\n```\n\nif you want to use gioc aop,you have to code the general aop implement for interface.but no surprise, this suck code can auto generated by tool.notice that the aop implement is register by gioc.RegisterHook not gioc.Register\n\n```go\nfunc hookHandler(data interface{}) interface{} {\n\tdataValue := reflect.ValueOf(data)\n\tnewValue := reflect.MakeFunc(dataValue.Type(), func(args []reflect.Value) []reflect.Value {\n\t\tfmt.Println(\"Hook Begin!\")\n\t\tresult := dataValue.Call(args)\n\t\tfmt.Println(\"Hook End!\")\n\t\treturn result\n\t})\n\treturn newValue.Interface()\n}\nhook := map[string]interface{}{\n\t\"UserAo.Get\": hookHandler,\n\t\"UserAo.Add\": hookHandler,\n}\nuserAo := gioc.New((*api.UserAo)(nil), nil, hook).(api.UserAo)\n```\n\nat last,use gioc.New to create bean,the third argument is hook setting.and now,we can never modify userAoImpl code to achieve aop!!\n\n# TODO\n\n* mutli thread support\n* Register for singleton\n* aop by regex expression like aspectJ and use complete package name\n* more elegant register interface\n* auto generate hook code\n\n# License\n\ngioc source code is licensed under the Apache Licence, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0.html)\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffishedee%2Fgioc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffishedee%2Fgioc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffishedee%2Fgioc/lists"}