The go-ctx library provides a framework for building modular applications with dependency injection, lifecycle management, and environment configuration.
- Service Lifecycle: Services can implement
Initializable,StartAware,StopAware, andDisposableinterfaces - Dependency Injection: By name, by type, or via reflection tags
- Environment Configuration: Inject environment variables with support for default values and complex types
- Logging: Integrated with
slogand loggers injection - Health Checks: Services can report health status
- Timers: Run periodic tasks with
TimerTask - Inter-Service Communication: Use
ServiceConnectorfor message passing - Reflection-Based Wiring: Automatic dependency injection using struct tags
- Panic Recovery: Utilities to handle panics gracefully
Use Go modules:
go get github.com/sedmess/go-ctxDefine a service:
package main
import (
"context"
"github.com/sedmess/go-ctx/ctx"
"github.com/sedmess/go-ctx/ctx/logger"
)
type HelloService struct {
ctx.AppContext `ctx:"CTX"`
l logger.Logger `ctx:""`
}
func (h *HelloService) AfterStart() {
h.l.Info("Hello, World!")
}
func main() {
app := ctx.CreateContextualizedApplication(
ctx.PackageOf(&HelloService{}),
)
app.Join()
}type aService struct {
paramA int
}
func (instance *aService) Init(_ ctx.ServiceProvider) {
instance.paramA = ctx.GetEnv(paramAName).AsIntDefault(5)
logger.Info(instance.Name(), "initialized")
}
func (instance *aService) Name() string {
return "a_service"
}type timedService struct {
ctx.TimerTask
l logger.Logger `ctx:""`
}
func (instance *timedService) AfterStart() {
instance.l.Info("Starting timer")
instance.StartTimer(2*time.Second, func() {
logger.Warn("timer", "onTimer!")
})
}
func (instance *timedService) BeforeStop() {
instance.StopTimer()
}type envInjectDemoService struct {
Duration time.Duration `env:"DURATION"`
}type reflectiveSingletonServiceImpl struct {
L logger.Logger `ctx:"singleton"`
A *aService `ctx:"a_service"`
}_ = os.Setenv("SLOG_LEVEL", "debug")
_ = os.Setenv("SLOG_ADD_SOURCE", "true")
ctx.SetSlogWriter(os.Stdout, os.Stderr)func (instance *aService) Health() health.ServiceHealth {
return health.Status(health.Up)
}type newTags struct {
l1 logger.Logger `ctx:""`
l2 logger.Logger `ctx:"named_logger_2"`
}type slogExample struct {
l1 *slog.Logger `ctx:""`
l2 *slog.Logger `ctx:"named_slog1"`
}go run main_example.goMIT