Skip to content

Simple dependency injection service container in golang

License

Notifications You must be signed in to change notification settings

centrexbd/gocontainer

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go-container

GoDoc Go Report Card

Simple dependency injection service container for golang

Usage

// app/main.go
package main

import (
        "fmt"
        "log"

        "github.com/ncrypthic/gocontainer"
)

type Config struct {
        UserServiceUrl string
}

type Service struct {
        // Will be injected by service container
        Config config.Config `inject:"config"`
}

func main() {
        config := Config{
                UserServiceUrl: "http://example.com/users",
        }
        // no need to manually pass config to user.Service struct
        userService := new(user.Service)
        container := gocontainer.NewContainer()
        container.RegisterService("config", config)
        container.RegisterService("userService", userService)
        /* To allow service container handling the application process
           exit and graceful shutdown, just uncomment the following line */
        // container.EnableGracefulShutdown(25 * time.Second )

        // Populate and inject dependencies
        if err := container.Ready(); err != nil {
                log.Fatalf("Failed to populate service container! %v", err)
        }
        // http.ListenAndServe(":8080", nil)
}

Service container allow clean intialization file by injecting dependencies to every services in the container.

License

MIT License

About

Simple dependency injection service container in golang

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%