1- # Wire Tutorial
1+ # Autowire Tutorial
22
3- Let's learn to use Wire by example. The [ Wire guide] [ guide ] provides thorough
4- documentation of the tool's usage. For readers eager to see Wire applied to a
5- larger server, the [ guestbook sample in Go Cloud] [ guestbook ] uses Wire to
3+ Let's learn to use Autowire by example. The [ Autowire guide] [ guide ] provides thorough
4+ documentation of the tool's usage. For readers eager to see Autowire applied to a
5+ larger server, the [ guestbook sample in Go Cloud] [ guestbook ] uses Autowire to
66initialize its components. Here we are going to build a small greeter program to
7- understand how to use Wire . The finished product may be found in the same
7+ understand how to use Autowire . The finished product may be found in the same
88directory as this README.
99
1010[ guestbook ] : https://github.com/google/go-cloud/tree/master/samples/guestbook
11- [ guide ] : https://github.com/google/wire /blob/master/docs/guide.md
11+ [ guide ] : https://github.com/google/autowire /blob/master/docs/guide.md
1212
1313## A First Pass of Building the Greeter Program
1414
@@ -88,7 +88,7 @@ The `Start` method holds the core of our small application: it tells the
8888greeter to issue a greeting and then prints that message to the screen.
8989
9090Now that we have all the components of our application ready, let's see what it
91- takes to initialize all the components without using Wire . Our main function
91+ takes to initialize all the components without using Autowire . Our main function
9292would look like this:
9393
9494``` go
@@ -112,10 +112,10 @@ dependency with another.
112112
113113[ di ] : https://stackoverflow.com/questions/130794/what-is-dependency-injection
114114
115- ## Using Wire to Generate Code
115+ ## Using Autowire to Generate Code
116116
117117One downside to dependency injection is the need for so many initialization
118- steps. Let's see how we can use Wire to make the process of initializing our
118+ steps. Let's see how we can use Autowire to make the process of initializing our
119119components smoother.
120120
121121Let's start by changing our ` main ` function to look like this:
@@ -128,53 +128,54 @@ func main() {
128128}
129129```
130130
131- Next, in a separate file called ` wire .go` we will define ` InitializeEvent ` .
131+ Next, in a separate file called ` autowire .go` we will define ` InitializeEvent ` .
132132This is where things get interesting:
133133
134134``` go
135- // wire .go
135+ // autowire .go
136136
137137func InitializeEvent () Event {
138- wire .Build (NewEvent, NewGreeter, NewMessage)
138+ autowire .Build (NewEvent, NewGreeter, NewMessage)
139139 return Event{}
140140}
141141```
142142
143143Rather than go through the trouble of initializing each component in turn and
144- passing it into the next one, we instead have a single call to ` wire .Build`
145- passing in the initializers we want to use. In Wire , initializers are known as
144+ passing it into the next one, we instead have a single call to ` autowire .Build`
145+ passing in the initializers we want to use. In Autowire , initializers are known as
146146"providers," functions which provide a particular type. We add a zero value for
147147` Event ` as a return value to satisfy the compiler. Note that even if we add
148- values to ` Event ` , Wire will ignore them. In fact, the injector's purpose is to
148+ values to ` Event ` , Autowire will ignore them. In fact, the injector's purpose is to
149149provide information about which providers to use to construct an ` Event ` and so
150150we will exclude it from our final binary with a build constraint at the top of
151151the file:
152152
153153``` go
154154// +build wireinject
155+ // go:build wireinject
155156
156157```
157158
158159Note, a [ build constraint] [ constraint ] requires a blank, trailing line.
159160
160- In Wire parlance, ` InitializeEvent ` is an "injector." Now that we have our
161- injector complete, we are ready to use the ` wire ` command line tool.
161+ In Autowire parlance, ` InitializeEvent ` is an "injector." Now that we have our
162+ injector complete, we are ready to use the ` autowire ` command line tool.
162163
163164Install the tool with:
164165
165166``` shell
166- go get github.com/google/wire /cmd/wire
167+ go get github.com/dabbertorres/autowire /cmd/autowire
167168```
168169
169- Then in the same directory with the above code, simply run ` wire ` . Wire will
170+ Then in the same directory with the above code, simply run ` autowire ` . Autowire will
170171find the ` InitializeEvent ` injector and generate a function whose body is
171172filled out with all the necessary initialization steps. The result will be
172- written to a file named ` wire_gen .go` .
173+ written to a file named ` autowire_gen .go` .
173174
174- Let's take a look at what Wire did for us:
175+ Let's take a look at what Autowire did for us:
175176
176177``` go
177- // wire_gen .go
178+ // autowire_gen .go
178179
179180func InitializeEvent () Event {
180181 message := NewMessage ()
@@ -186,14 +187,14 @@ func InitializeEvent() Event {
186187
187188It looks just like what we wrote above! Now this is a simple example with just
188189three components, so writing the initializer by hand isn't too painful. Imagine
189- how useful Wire is for components that are much more complex. When working with
190- Wire , we will commit both ` wire .go` and ` wire_gen .go` to source control.
190+ how useful Autowire is for components that are much more complex. When working with
191+ Autowire , we will commit both ` autowire .go` and ` autowire_gen .go` to source control.
191192
192193[ constraint ] : https://godoc.org/go/build#hdr-Build_Constraints
193194
194- ## Making Changes with Wire
195+ ## Making Changes with Autowire
195196
196- To show a small part of how Wire handles more complex setups, let's refactor
197+ To show a small part of how Autowire handles more complex setups, let's refactor
197198our initializer for ` Event ` to return an error and see what happens.
198199
199200``` go
@@ -251,21 +252,21 @@ func main() {
251252We also need to update ` InitializeEvent ` to add an ` error ` type to the return value:
252253
253254``` go
254- // wire .go
255+ // autowire .go
255256
256257func InitializeEvent () (Event , error ) {
257- wire .Build (NewEvent, NewGreeter, NewMessage)
258+ autowire .Build (NewEvent, NewGreeter, NewMessage)
258259 return Event{}, nil
259260}
260261```
261262
262- With the setup complete, we are ready to invoke the ` wire ` command again. Note,
263- that after running ` wire ` once to produce a ` wire_gen .go` file, we may also use
264- ` go generate ` . Having run the command, our ` wire_gen .go` file looks like
263+ With the setup complete, we are ready to invoke the ` autowire ` command again. Note,
264+ that after running ` autowire ` once to produce a ` autowire_gen .go` file, we may also use
265+ ` go generate ` . Having run the command, our ` autowire_gen .go` file looks like
265266this:
266267
267268``` go
268- // wire_gen .go
269+ // autowire_gen .go
269270
270271func InitializeEvent () (Event , error ) {
271272 message := NewMessage ()
@@ -278,21 +279,21 @@ func InitializeEvent() (Event, error) {
278279}
279280```
280281
281- Wire has detected that the ` NewEvent ` provider may fail and has done the right
282+ Autowire has detected that the ` NewEvent ` provider may fail and has done the right
282283thing inside the generated code: it checks the error and returns early if one
283284is present.
284285
285286## Changing the Injector Signature
286287
287- As another improvement, let's look at how Wire generates code based on the
288+ As another improvement, let's look at how Autowire generates code based on the
288289signature of the injector. Presently, we have hard-coded the message inside
289290` NewMessage ` . In practice, it's much nicer to allow callers to change that
290291message however they see fit. So let's change ` InitializeEvent ` to look like
291292this:
292293
293294``` go
294295func InitializeEvent (phrase string ) (Event , error ) {
295- wire .Build (NewEvent, NewGreeter, NewMessage)
296+ autowire .Build (NewEvent, NewGreeter, NewMessage)
296297 return Event{}, nil
297298}
298299```
@@ -306,12 +307,12 @@ func NewMessage(phrase string) Message {
306307}
307308```
308309
309- After we run ` wire ` again, we will see that the tool has generated an
310+ After we run ` autowire ` again, we will see that the tool has generated an
310311initializer which passes the ` phrase ` value as a ` Message ` into ` Greeter ` .
311312Neat!
312313
313314``` go
314- // wire_gen .go
315+ // autowire_gen .go
315316
316317func InitializeEvent (phrase string ) (Event , error ) {
317318 message := NewMessage (phrase)
@@ -324,44 +325,44 @@ func InitializeEvent(phrase string) (Event, error) {
324325}
325326```
326327
327- Wire inspects the arguments to the injector, sees that we added a string to the
328+ Autowire inspects the arguments to the injector, sees that we added a string to the
328329list of arguments (e.g., ` phrase ` ), and likewise sees that among all the
329330providers, ` NewMessage ` takes a string, and so it passes ` phrase ` into
330331` NewMessage ` .
331332
332333## Catching Mistakes with Helpful Errors
333334
334- Let's also look at what happens when Wire detects mistakes in our code and see
335- how Wire 's error messages help us correct any problems.
335+ Let's also look at what happens when Autowire detects mistakes in our code and see
336+ how Autowire 's error messages help us correct any problems.
336337
337338For example, when writing our injector ` InitializeEvent ` , let's say we forget
338339to add a provider for ` Greeter ` . Let's see what happens:
339340
340341``` go
341342func InitializeEvent (phrase string ) (Event , error ) {
342- wire .Build (NewEvent, NewMessage) // woops! We forgot to add a provider for Greeter
343+ autowire .Build (NewEvent, NewMessage) // woops! We forgot to add a provider for Greeter
343344 return Event{}, nil
344345}
345346```
346347
347- Running ` wire ` , we see the following:
348+ Running ` autowire ` , we see the following:
348349
349350``` shell
350351# wrapping the error across lines for readability
351- $GOPATH /src/github.com/google/wire /_tutorial/wire .go:24:1:
352- inject InitializeEvent: no provider found for github.com/google/wire /_tutorial.Greeter
353- (required by provider of github.com/google/wire /_tutorial.Event)
354- wire : generate failed
352+ $GOPATH /src/github.com/google/autowire /_tutorial/autowire .go:24:1:
353+ inject InitializeEvent: no provider found for github.com/google/autowire /_tutorial.Greeter
354+ (required by provider of github.com/google/autowire /_tutorial.Event)
355+ autowire : generate failed
355356```
356357
357- Wire is telling us some useful information: it cannot find a provider for
358+ Autowire is telling us some useful information: it cannot find a provider for
358359` Greeter ` . Note that the error message prints out the full path to the
359360` Greeter ` type. It's also telling us the line number and injector name where
360361the problem occurred: line 24 inside ` InitializeEvent ` . In addition, the error
361362message tells us which provider needs a ` Greeter ` . It's the ` Event ` type. Once
362363we pass in a provider of ` Greeter ` , the problem will be solved.
363364
364- Alternatively, what happens if we provide one too many providers to ` wire .Build` ?
365+ Alternatively, what happens if we provide one too many providers to ` autowire .Build` ?
365366
366367``` go
367368func NewEventNumber () int {
@@ -370,50 +371,50 @@ func NewEventNumber() int {
370371
371372func InitializeEvent (phrase string ) (Event , error ) {
372373 // woops! NewEventNumber is unused.
373- wire .Build (NewEvent, NewGreeter, NewMessage, NewEventNumber)
374+ autowire .Build (NewEvent, NewGreeter, NewMessage, NewEventNumber)
374375 return Event{}, nil
375376}
376377```
377378
378- Wire helpfully tells us that we have an unused provider:
379+ Autowire helpfully tells us that we have an unused provider:
379380
380381``` shell
381- $GOPATH /src/github.com/google/wire /_tutorial/wire .go:24:1:
382+ $GOPATH /src/github.com/google/autowire /_tutorial/autowire .go:24:1:
382383inject InitializeEvent: unused provider " NewEventNumber"
383- wire : generate failed
384+ autowire : generate failed
384385```
385386
386- Deleting the unused provider from the call to ` wire .Build` resolves the error.
387+ Deleting the unused provider from the call to ` autowire .Build` resolves the error.
387388
388389## Conclusion
389390
390391Let's summarize what we have done here. First, we wrote a number of components
391392with corresponding initializers, or providers. Next, we created an injector
392393function, specifying which arguments it receives and which types it returns.
393- Then, we filled in the injector function with a call to ` wire .Build` supplying
394- all necessary providers. Finally, we ran the ` wire ` command to generate code
394+ Then, we filled in the injector function with a call to ` autowire .Build` supplying
395+ all necessary providers. Finally, we ran the ` autowire ` command to generate code
395396that wires up all the different initializers. When we added an argument to the
396- injector and an error return value, running ` wire ` again made all the necessary
397+ injector and an error return value, running ` autowire ` again made all the necessary
397398updates to our generated code.
398399
399- The example here is small, but it demonstrates some of the power of Wire , and
400+ The example here is small, but it demonstrates some of the power of Autowire , and
400401how it takes much of the pain out of initializing code using dependency
401- injection. Furthermore, using Wire produced code that looks much like what we
402- would otherwise write. There are no bespoke types that commit a user to Wire .
402+ injection. Furthermore, using Autowire produced code that looks much like what we
403+ would otherwise write. There are no bespoke types that commit a user to Autowire .
403404Instead it's just generated code. We may do with it what we will. Finally,
404405another point worth considering is how easy it is to add new dependencies to
405- our component initialization. As long as we tell Wire how to provide (i.e.,
406+ our component initialization. As long as we tell Autowire how to provide (i.e.,
406407initialize) a component, we may add that component anywhere in the dependency
407- graph and Wire will handle the rest.
408+ graph and Autowire will handle the rest.
408409
409- In closing, it is worth mentioning that Wire supports a number of additional
410+ In closing, it is worth mentioning that Autowire supports a number of additional
410411features not discussed here. Providers may be grouped in [ provider sets] [ sets ] .
411412There is support for [ binding interfaces] [ interfaces ] , [ binding
412413values] [ values ] , as well as support for [ cleanup functions] [ cleanup ] . See the
413414[ Advanced Features] [ advanced ] section for more.
414415
415- [ advanced ] : https://github.com/google/wire /blob/master/docs/guide.md#advanced-features
416- [ cleanup ] : https://github.com/google/wire /blob/master/docs/guide.md#cleanup-functions
417- [ interfaces ] : https://github.com/google/wire /blob/master/docs/guide.md#binding-interfaces
418- [ sets ] : https://github.com/google/wire /blob/master/docs/guide.md#defining-providers
419- [ values ] : https://github.com/google/wire /blob/master/docs/guide.md#binding-values
416+ [ advanced ] : https://github.com/google/autowire /blob/master/docs/guide.md#advanced-features
417+ [ cleanup ] : https://github.com/google/autowire /blob/master/docs/guide.md#cleanup-functions
418+ [ interfaces ] : https://github.com/google/autowire /blob/master/docs/guide.md#binding-interfaces
419+ [ sets ] : https://github.com/google/autowire /blob/master/docs/guide.md#defining-providers
420+ [ values ] : https://github.com/google/autowire /blob/master/docs/guide.md#binding-values
0 commit comments