Hello Gophers!
I’m excited to share Gone, a lightweight dependency injection framework for Go that uses struct tags to simplify your application architecture.
What is Gone?
Gone is a tag-based dependency injection framework that makes it easy to manage dependencies in your Go applications. It’s designed to be lightweight, intuitive, and powerful, allowing you to focus on your business logic rather than wiring components together.
Key Features
- Simple Tag-based Injection: Use struct tags to declare dependencies
- Support for Private Field Injection: Inject dependencies into private fields
- Function Parameter Injection: Inject by function parameter type
- Provider Mechanism: Inject external components into your application
- Lifecycle Management: Define initialization methods and hooks for your components
- Service Support: Automatically manage service start and stop operations
- Code Generation: Automatically complete component registration with Gonectr
- Interface-based Mock Testing: Easily create mocks for unit testing
Quick Example
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()
}
Getting Started
- Install the required tools:
go install github.com/gone-io/gonectr@latest
go install go.uber.org/mock/mockgen@latest
- Create a new project:
gonectr create myproject
- Run your project:
cd myproject
go mod tidy
gonectr run ./cmd/server
Why Choose Gone?
- Lightweight: Minimal overhead and dependencies
- Intuitive: Simple tag-based approach that’s easy to understand
- Flexible: Works with both new and existing codebases
- Testable: Makes unit testing straightforward with interface-based mocks
- Production-Ready: Used in real-world applications
Learn More
We’re excited to see what you build with Gone! Feel free to ask questions, report issues, or contribute to the project.
License
Gone is released under the MIT License.