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

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go-container

GoDoc Go Report Card

Simple dependency injection service container for golang

Usage

Suppose we have the following files:

  • app/config/config.go => Configuration values collection

    // app/config/config.go
    package config
    
    type Config struct {
            UserServiceUrl string
    }
  • app/user/service.go => Service to manipulate users through external endpoint

    // app/user/service.go
    package user
    
    import (
            "app/config"
    )
    
    type Service struct {
            // Will be injected
            Config config.Config `inject:"config"`
    }
    
    func (svc *Service) GetUserByID(id string) error {
            _, err := http.Get(fmt.Sprintf("%s/%s", svc.Config.SomeUrl, id))
            return err
    }
  • app/main.go => application main file

// app/main.go
package main

import (
	"fmt"
        "log"

	"github.com/ncrypthic/gocontainer"

        "app/config"
        "app/user"
)

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)
        // Populate and inject dependencies
	if err := container.Ready(); err != nil {
                log.Fatalf("Failed to populate service container! %v", err)
        }
        // do GET request to http://example.com/users/some-id
	userService.GetUserByID("some-id")
}

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%