Cargo
:package: A Go dependency injection container that promotes low coupling and inversion of control
Install / Use
/learn @drgomesp/CargoREADME
DEPRECATION NOTICE: For a better, production-ready compile-time dependency injection library, use https://github.com/google/wire.
drgomesp/cargo
An efficient and robust Go dependency injection container – by Daniel Ribeiro
Table of Contents
Introduction
cargo is a library that provides a powerful way of handling objects and their dependencies, by using the Container. The container works by implementing the Dependency Injection pattern via constructor injection, resulting in explicit dependencies and the achievement of the Inversion of Control principle.
Installation
$ go get github.com/drgomesp/cargo
Getting Started
Creating/Registering Services
There are two main methods used to define services in the container: container.Register
and container.Set. The first one assumes you already have a pointer to an object instance
to work with, the second needs a definition.
Suppose you have an object:
type HttpClient struct {}
client := new(HttpClient)
To define that as a service, all you need to do is:
dic := container.NewContainer()
dic.Set("http.client", client)
From now on, whenever you need to work with the http client service, you can simply do:
if s, err := dic.Get("http.client"); err != nil {
panic(err)
}
client := s.(*HttpClient) // the type assertion is required
Or, if you do not need errors handling and panic is fine, you can get the same behavior with short synthax:
client := dic.MustGet("http.client").(*HttpClient)
Related Skills
node-connect
347.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
xurl
347.2kA CLI tool for making authenticated requests to the X (Twitter) API. Use this skill when you need to post tweets, reply, quote, search, read posts, manage followers, send DMs, upload media, or interact with any X API v2 endpoint.
frontend-design
108.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
347.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
