-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yoyofx
committed
Jul 12, 2021
1 parent
4c3dfd1
commit 20d9513
Showing
1 changed file
with
21 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,24 @@ | ||
# DI | ||
Dependency injection for Go programming language. | ||
|
||
Dependency injection is one form of the broader technique of inversion of control. It is used to increase modularity of the program and make it extensible. | ||
Dependency injection is one form of the broader technique of inversion of control. It is used to increase modularity of the program and make it extensible. | ||
|
||
## Examples | ||
```go | ||
type A struct { | ||
Name string | ||
} | ||
|
||
func NewA() *A { | ||
r := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
name := "A-" + strconv.Itoa(r.Int()) | ||
return &A{Name: ls} | ||
} | ||
|
||
services := NewServiceCollection() | ||
services.AddSingleton(NewA) | ||
serviceProvider := services.Build() | ||
|
||
var env *A | ||
_ = serviceProvider.GetService(&env) // used | ||
``` |