Skip to content

Latest commit

 

History

History
52 lines (40 loc) · 834 Bytes

README.md

File metadata and controls

52 lines (40 loc) · 834 Bytes

opje

Golang Service Locator Thingy

Usage

package main

import (
	"fmt"
	"opje"
)

type AuthService interface {
	Login(username, password string) (bool, error)
	Register(username, password string) (bool, error)
}

type authentication struct{}

func newAuthService() AuthService {
	return &authentication{}
}

func (a *authentication) Login(username, password string) (bool, error) {
	return true, nil
}

func (a *authentication) Register(username, password string) (bool, error) {
	return true, nil
}

func init() {
	locator.Register(newAuthService())
}

func main() {
	auth, err := locator.Resolve[AuthService]()
	if err != nil {
		panic("authService not registered")
	}

	loggedin, err := auth.Login("admin", "admin")
	if err != nil {
		panic("login failed")
	}

	if loggedin {
		fmt.Println("Logged in ✌️")
	}
}