https://github.com/eddieowens/axon
A simple, lightweight, and lazy-loaded DI (really just a singleton management) library.
https://github.com/eddieowens/axon
dependency-injection generics go golang
Last synced: 5 months ago
JSON representation
A simple, lightweight, and lazy-loaded DI (really just a singleton management) library.
- Host: GitHub
- URL: https://github.com/eddieowens/axon
- Owner: eddieowens
- License: apache-2.0
- Created: 2018-10-22T21:53:50.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-05-23T01:33:11.000Z (about 4 years ago)
- Last Synced: 2024-06-19T00:04:03.995Z (almost 2 years ago)
- Topics: dependency-injection, generics, go, golang
- Language: Go
- Homepage:
- Size: 52.7 KB
- Stars: 22
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# axon
[](https://goreportcard.com/report/github.com/eddieowens/axon)
[](https://github.com/eddieowens/axon/blob/master/LICENSE)
[](https://pkg.go.dev/github.com/eddieowens/axon?tab=doc)
A simple, lightweight, and lazy-loaded DI (really just a singleton management) library that supports generics.
Influenced by multiple DI
libraries but more specifically Google's [Guice](https://github.com/google/guice).
## Install
```bash
go get github.com/eddieowens/axon
```
## Usage
### Basic
Simple add and get with a string key
```go
package main
import (
"fmt"
"github.com/eddieowens/axon"
)
func main() {
axon.Add("AnswerToTheUltimateQuestion", 42)
answer := axon.MustGet(axon.WithKey("AnswerToTheUltimateQuestion"))
fmt.Println(answer) // prints 42
}
```
You can also use a type as a key
```go
package main
import (
"fmt"
"github.com/eddieowens/axon"
)
func main() {
axon.Add(axon.NewTypeKey[int](42))
answer := axon.MustGet[int]()
fmt.Println(answer) // prints 42
}
```
### Injecting dependencies
To inject dependencies to a struct, you can use the `Inject` func.
```go
package main
import (
"fmt"
"github.com/eddieowens/axon"
)
type Struct struct {
Answer int `inject:"AnswerToTheUltimateQuestion"`
}
func main() {
axon.Add("AnswerToTheUltimateQuestion", 42)
s := new(Struct)
_ = axon.Inject(s)
fmt.Println(s.Answer) // prints 42
}
```
A more full fledged example
```go
package main
import (
"fmt"
"github.com/eddieowens/axon"
"os"
)
type DatabaseClient interface {
DeleteUser(user string) error
}
type databaseClient struct {
}
func (d *databaseClient) DeleteUser(_ string) error {
fmt.Println("Deleting user!")
return nil
}
type ServerConfig struct {
Port int `inject:"port"`
}
type Server struct {
// inject whatever is the default for the DBClient type
DB DatabaseClient `inject:",type"`
ServerConfig ServerConfig `inject:"config"`
}
func main() {
axon.Add("port", os.Getenv("PORT"))
// default implementation for DatabaseClient
axon.Add(axon.NewTypeKey[DatabaseClient](new(databaseClient)))
// construct the Config whenever it's needed (only ever called once)
axon.Add("config", axon.NewFactory[ServerConfig](func(_ axon.Injector) (ServerConfig, error) {
return ServerConfig{}, nil
}))
s := new(Server)
_ = axon.Inject(s)
fmt.Println(s.ServerConfig.Port) // prints value of env var PORT
fmt.Println(s.DB.DeleteUser("user")) // prints Deleting user!
}
```
For more examples and info, check out the [GoDoc](https://pkg.go.dev/github.com/eddieowens/axon?tab=doc)