Skip to content

rytsh/call

Folders and files

NameName
Last commit message
Last commit date

Latest commit

71dc03b · Sep 20, 2024

History

9 Commits
Sep 20, 2024
Nov 19, 2022
Sep 25, 2022
Sep 20, 2024
Sep 20, 2024
Mar 20, 2023
Mar 20, 2023
Nov 27, 2022
Nov 27, 2022
Sep 25, 2022
Sep 25, 2022
Nov 27, 2022
Nov 27, 2022
Nov 27, 2022
Nov 27, 2022
Nov 27, 2022
Nov 27, 2022
Sep 25, 2022
Sep 25, 2022

Repository files navigation

call

License Coverage GitHub Workflow Status Go Report Card Web

Call dependency injection library based on registry arguments and functions.

go get github.com/rytsh/call

Usage

First get new registry and add own functions and arguments.

Also you can add arguments in directly function.

Registry add function and arguments not in order you can add argument later.

// create registry
reg := call.NewReg().
    AddArgument("a", 6).
    AddArgument("b", 2).
    AddFunction("divide", func(a, b int) (int, error) {
        if b == 0 {
            return 0, fmt.Errorf("divide by zero")
        }

        return a / b, nil
    }, "a", "b")

// call function
returns, err := reg.Call("divide")
if err != nil {
    fmt.Println(err)

    return
}

fmt.Println(returns[0], returns[1])
// Output:
// 3 <nil>

It is possible to call directly with arguments also callable as variadic.

returns, err := reg.CallWithArgs("divide", "a", "b")

If function's argument length and type is not match, it will return error.

Check documentation for more details.