English ο½ δΈζ
Gone is a lightweight golang dependency injection framework. Here's a simple example (a structure that embeds gone.Flag, which we call a "Goner"):
package main
import "github.com/gone-io/gone/v2"
type Dep struct {
gone.Flag
Name string
}
type Component struct {
gone.Flag
dep *Dep `gone:"*"` //dependency injection
log gone.Logger `gone:"*"`
}
func (c *Component) Init() {
c.log.Infof(c.dep.Name) //using dependency
}
func main() {
gone.
NewApp().
// Register and load components
Load(&Dep{Name: "Component Dep"}).
Load(&Component{}).
//run
Run()
}
- Supports struct property injection, including private field injection ππ» Dependency Injection Introduction
- Supports function parameter injection, injecting by function parameter type ππ» Dependency Injection Introduction
- Provider mechanism, supports injecting external components into Goners: ππ» Gone V2 Provider Mechanism Introduction
- Supports code generation, automatically completing component registration and loading (via Gonectr)
- Supports interface-based mock unit testing
- Supports Goner components for microservice development
- Supports defining initialization methods
Init()
andBeforeInit()
for Goners - Supports writing service-type Goners: implementing
Start() error
andStop() error
, the framework will automatically call Start() and Stop() methods - Supports hooks like
BeforeStart
,AfterStart
,BeforeStop
,AfterStop
for executing custom operations when services start and stop
- Install gonectr and mockgen
go install github.com/gone-io/gonectr@latest go install go.uber.org/mock/mockgen@latest
- Create a project
gonectr create myproject
- Run the project
cd myproject go mod tidy gonectr run ./cmd/server
- Added
option:"lazy"
tag to support lazy field injection, see documentation - Note: Fields marked with
option:"lazy"
cannot be used in theInit
,Provide
, andInject
methods
- Added SetValue function for unified handling of various configuration value types
- Refactored existing type handling logic, using reflection to improve generality
- Added
option:"allowNil"
tag to support graceful handling of optional dependencies - Improved tests and documentation
Version 2 has been extensively updated, removing unnecessary concepts. Please refer to: Gone@v2 Instructions before use.
- Defined gone.Provider, a factory function for injecting external components (structs, struct pointers, functions, interfaces) that are not Goners into Goner properties requiring injection
- Fixed
gone.NewProviderPriest
which couldn't generate Priests for interface-type gone.Provider - Wrote test code for
goner/gorm
, completed other test codes; documentation updated.
- Provided a new
gone.GonerOption
that can inject by type, delegating the task of constructing type instances to a Goner that implementsSuck(conf string, v reflect.Value, field reflect.StructField) error
- Provided a helper function for implementing Goner Provider:
func NewProviderPriest[T any, P any](fn func(tagConf string, param P) (T, error)) Priest
- Provided strategy configuration for cluster mode in
goner/xorm
- Improved
goner/gorm
code and functional testing, supporting connection to various databases.
- goner/xorm supports clusters and multiple databases, latest documentation: https://goner.fun/zh/references/xorm.html
- Added goner/gorm, encapsulating
gorm.io/gorm
for database access, currently only supports MySQL, in progress...
If you find errors or have feature requests, feel free to submit an issue, and pull requests are welcome.
If you have any questions, please contact us through:
gone
is released under the MIT License, please see the LICENSE file for details.