# Dependency Injection

Copper uses `google/wire` to enable dependency injection. Check out the [introductory blog post](https://go.dev/blog/wire) to learn more about dependency injection and Wire.

In a Copper project, each package has a `wire.go` file. This file is autogenerated if you use the Copper CLI to create your package directories.

```bash
copper scaffold:pkg <package>
```

### Example

Let's say you have the following struct that requires `rockets.Queries` and `clogger.Logger` for its methods.

```go
type Launcher struct {
    rockets *rockets.Queries
    logger  clogger.Logger
}

func (l *Launcher) Launch(ctx context.Context, rocketID string) error {
    // ...    
}
```

To enable automating wiring up of all dependencies, create a constructor for the `Launcher` struct.

```go
func NewLauncher(r *rockets.Queries, l clogger.Logger) *Launcher {
  return &Launcher{
    rockets: r,
    logger:  l,
  }
}
```

Finally, add the constructor to the package's `WireModule` defined in the `wire.go` file.

```go
var WireModule = wire.NewSet(
    NewLauncher,
)
```

That's it! Copper + Wire will automatically pass in the correct dependencies to the `NewLauncher` constructor.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gocopper.gitbook.io/copper/the-basics/dependency-injection.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
