aboutsummaryrefslogtreecommitdiff
path: root/content/wire.article
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2020-03-09 23:54:35 -0400
committerRuss Cox <rsc@golang.org>2020-03-17 20:58:37 +0000
commitaf5018f64e406aaa646dae066f28de57321ea5ce (patch)
tree8db7b1f049d83d215fa9abf68851efce7b5ccadb /content/wire.article
parent86e424fac66fa90ddcb7e8d7febd4c2b07d7c59e (diff)
content: convert to Markdown-enabled present inputs
Converted blog to Markdown-enabled present (CL 222846) using present2md (CL 222847). For golang/go#33955. Change-Id: Ib39fa1ddd9a46f9c7a62a2ca7b96e117635553e8 Reviewed-on: https://go-review.googlesource.com/c/blog/+/222848 Run-TryBot: Russ Cox <rsc@golang.org> Reviewed-by: Andrew Bonventre <andybons@golang.org>
Diffstat (limited to 'content/wire.article')
-rw-r--r--content/wire.article79
1 files changed, 40 insertions, 39 deletions
diff --git a/content/wire.article b/content/wire.article
index 2d9ca5b..5113228 100644
--- a/content/wire.article
+++ b/content/wire.article
@@ -1,19 +1,20 @@
-Compile-time Dependency Injection With Go Cloud's Wire
+# Compile-time Dependency Injection With Go Cloud's Wire
9 Oct 2018
+Summary: The Go team recently [announced](https://blog.golang.org/go-cloud) the open source project [Go Cloud](https://github.com/google/go-cloud), with portable Cloud APIs and tools for [open cloud](https://cloud.google.com/open-cloud/) development. This post goes into more detail about Wire, a dependency injection tool used in Go Cloud.
Robert van Gent
-* Overview
+## Overview
-The Go team recently [[https://blog.golang.org/go-cloud][announced]] the
-open source project [[https://github.com/google/go-cloud][Go Cloud]],
-with portable Cloud APIs and tools for [[https://cloud.google.com/open-cloud/][open cloud]] development.
+The Go team recently [announced](https://blog.golang.org/go-cloud) the
+open source project [Go Cloud](https://github.com/google/go-cloud),
+with portable Cloud APIs and tools for [open cloud](https://cloud.google.com/open-cloud/) development.
This post goes into more detail about Wire,
a dependency injection tool used in Go Cloud.
-* What problem does Wire solve?
+## What problem does Wire solve?
-[[https://en.wikipedia.org/wiki/Dependency_injection][Dependency injection]]
+[Dependency injection](https://en.wikipedia.org/wiki/Dependency_injection)
is a standard technique for producing flexible and loosely coupled code,
by explicitly providing components with all of the dependencies they need to work.
In Go, this often takes the form of passing dependencies to constructors:
@@ -43,21 +44,21 @@ or adding or removing an initializer,
and then let Wire do the tedious work of generating initialization code
for the entire dependency graph.
-* Why is this part of Go Cloud?
+## Why is this part of Go Cloud?
Go Cloud's goal is to make it easier to write portable Cloud applications
by providing idiomatic Go APIs for useful Cloud services.
-For example, [[https://godoc.org/github.com/google/go-cloud/blob][blob.Bucket]]
+For example, [blob.Bucket](https://godoc.org/github.com/google/go-cloud/blob)
provides a storage API with implementations for Amazon's S3 and Google Cloud Storage (GCS);
applications written using `blob.Bucket` can swap implementations without
changing their application logic.
However, the initialization code is inherently provider-specific,
and each provider has a different set of dependencies.
-For example, [[https://godoc.org/github.com/google/go-cloud/blob/gcsblob#OpenBucket][constructing a GCS `blob.Bucket`]]
+For example, [constructing a GCS `blob.Bucket`](https://godoc.org/github.com/google/go-cloud/blob/gcsblob#OpenBucket)
requires a `gcp.HTTPClient`,
which eventually requires `google.Credentials`,
-while [[https://godoc.org/github.com/google/go-cloud/blob/s3blob][constructing one for S3]]
+while [constructing one for S3](https://godoc.org/github.com/google/go-cloud/blob/s3blob)
requires an `aws.Config`,
which eventually requires AWS credentials.
Thus, updating an application to use a different `blob.Bucket` implementation
@@ -66,34 +67,34 @@ The driving use case for Wire is to make it easy to swap implementations
of Go Cloud portable APIs,
but it's also a general-purpose tool for dependency injection.
-* Hasn't this been done already?
+## Hasn't this been done already?
There are a number of dependency injection frameworks out there.
-For Go, [[https://github.com/uber-go/dig][Uber's dig]] and [[https://github.com/facebookgo/inject][Facebook's inject]]
+For Go, [Uber's dig](https://github.com/uber-go/dig) and [Facebook's inject](https://github.com/facebookgo/inject)
both use reflection to do runtime dependency injection.
-Wire was primarily inspired by Java's [[https://google.github.io/dagger/][Dagger 2]],
-and uses code generation rather than reflection or [[https://en.wikipedia.org/wiki/Service_locator_pattern][service locators]].
+Wire was primarily inspired by Java's [Dagger 2](https://google.github.io/dagger/),
+and uses code generation rather than reflection or [service locators](https://en.wikipedia.org/wiki/Service_locator_pattern).
We think this approach has several advantages:
-- Runtime dependency injection can be hard to follow and debug when the
- dependency graph gets complex.
- Using code generation means that the initialization code that's executed
- at runtime is regular,
- idiomatic Go code that's easy to understand and debug.
- Nothing is obfuscated by an intervening framework doing "magic".
- In particular, problems like forgetting a dependency become compile-time errors,
- not run-time errors.
-- Unlike [[https://en.wikipedia.org/wiki/Service_locator_pattern][service locators]],
- there's no need to make up arbitrary names or keys to register services.
- Wire uses Go types to connect components with their dependencies.
-- It's easier to avoid dependency bloat. Wire's generated code will only
- import the dependencies you need,
- so your binary won't have unused imports.
- Runtime dependency injectors can't identify unused dependencies until runtime.
-- Wire's dependency graph is knowable statically, which provides opportunities for tooling and visualization.
-
-* How does it work?
+ - Runtime dependency injection can be hard to follow and debug when the
+ dependency graph gets complex.
+ Using code generation means that the initialization code that's executed
+ at runtime is regular,
+ idiomatic Go code that's easy to understand and debug.
+ Nothing is obfuscated by an intervening framework doing "magic".
+ In particular, problems like forgetting a dependency become compile-time errors,
+ not run-time errors.
+ - Unlike [service locators](https://en.wikipedia.org/wiki/Service_locator_pattern),
+ there's no need to make up arbitrary names or keys to register services.
+ Wire uses Go types to connect components with their dependencies.
+ - It's easier to avoid dependency bloat. Wire's generated code will only
+ import the dependencies you need,
+ so your binary won't have unused imports.
+ Runtime dependency injectors can't identify unused dependencies until runtime.
+ - Wire's dependency graph is knowable statically, which provides opportunities for tooling and visualization.
+
+## How does it work?
Wire has two basic concepts: providers and injectors.
@@ -175,19 +176,19 @@ so writing the initializer by hand wouldn't be too painful,
but Wire saves a lot of manual toil for components and applications with
more complex dependency graphs.
-* How can I get involved and learn more?
+## How can I get involved and learn more?
-The [[https://github.com/google/wire/blob/master/README.md][Wire README]]
+The [Wire README](https://github.com/google/wire/blob/master/README.md)
goes into more detail about how to use Wire and its more advanced features.
-There's also a [[https://github.com/google/wire/tree/master/_tutorial][tutorial]]
+There's also a [tutorial](https://github.com/google/wire/tree/master/_tutorial)
that walks through using Wire in a simple application.
We appreciate any input you have about your experience with Wire!
-[[https://github.com/google/wire][Wire's]] development is conducted on GitHub,
-so you can [[https://github.com/google/wire/issues/new/choose][file an issue]]
+[Wire's](https://github.com/google/wire) development is conducted on GitHub,
+so you can [file an issue](https://github.com/google/wire/issues/new/choose)
to tell us what could be better.
For updates and discussion about the project,
-join [[https://groups.google.com/forum/#!forum/go-cloud][the Go Cloud mailing list]].
+join [the Go Cloud mailing list](https://groups.google.com/forum/#!forum/go-cloud).
Thank you for taking the time to learn about Go Cloud's Wire.
We’re excited to work with you to make Go the language of choice for developers