@@ -3,8 +3,6 @@ A dependency injection library using struct tags.
33
44- [ Slack channel] ( https://gophers.slack.com/messages/go-modules/ )
55
6- This project is currently alpha and subject to experimental change.
7-
86## Overview
97This library simplifies the wiring of an application by injecting dependencies between modules.
108
@@ -43,8 +41,10 @@ Fields may be set normally prior to binding.
4341module := struct {
4442 FieldA string ' provide:"provideMe"'
4543} {
46- FieldA : " providedValue "
44+ FieldA : " value "
4745}
46+ // or
47+ module.FieldA = " value"
4848```
4949
5050Modules implementing the * Provider* interface may set fields from the * Provide* method.
@@ -54,10 +54,11 @@ type module struct {
5454 Func func () string ' provide:"provideMe"'
5555}
5656// Implements modules.Provider
57- func (m *Module ) Provide () {
57+ func (m *Module ) Provide () error {
5858 m.Func = func () string {
5959 return = m.Field
6060 }
61+ return nil
6162}
6263```
6364The * Provide* method is called during binding. Injected fields have not yet necessarily been set when * Provide* is
@@ -72,6 +73,10 @@ type module struct {
7273 FieldC complex128 ' provide:"complexField" literal:"-1,1"'
7374}
7475```
76+ Other built-in tag keys include:
77+ - 'env' for environment variables
78+ - 'file' for os.File handles, and decoding of txt, json, xml, and gob
79+ - 'flag' for command line arguments
7580
7681### Binders
7782Modules are bound using a * Binder* . Binders are created with the * NewBinder* function, which optionally
@@ -95,15 +100,15 @@ The functional option *Injectors* can be used to map tag keys (anything besides
95100third party * Injector* s.
96101``` go
97102injectors := modules.Injectors (map [string ]Injector{
98- " customTag" : customTag. Injector ,
103+ " customTag" : customInjector ,
99104})
100105binder := modules.NewBinder (injectors)
101106module := struct {
102107 FieldA CustomType ' provide:"someField" customTag:"tagValueArgument"'
103108}
104109_ := binder.Bind (module)
105110```
106- When this module is bound, * customTag.Injector * may set the value of FieldA based on the tag value "tagValueArgument".
111+ When this module is bound, * customInjector * may set the value of FieldA based on the tag value "tagValueArgument".
107112
108113The * Injector* interface is defined in the inject package.
109114``` go
0 commit comments