Home > OS >  Wire google Inject with multi return from provider function
Wire google Inject with multi return from provider function

Time:12-02

Follow by example by google wire , we can init Event struct by

Message.go :

type Message string

func NewMessage() Message {
    //TBD    
}

Event.go

func NewEvent(g Message ) Event {
    return Event{Message : g}
}

type Event struct {
    Message message
}

func (e Event) Start() {
   fmt.Println(msg)
}

And we can init by wire :

func main() {
    e := InitializeEvent()
    e.Start()
}
    
func InitializeEvent() Event {
    wire.Build(NewEvent, NewMessage)
    return Event{}
}

Is there any way to work with init function return multi value but we only need one return value to inject, ex:

func NewMessage() (Message,error ){
    //TBD
}

or

func NewMessage() (Message,Greeter) {
    //TBD
}

CodePudding user response:

To declare a function with multiple return values, you need to put them in parentheses:

func NewMessage() (Message, error) {
    return Message(“TBD”), nil
}

EDIT: The question you're asking (whether you can return an error from the init functions) is answered in the next part of the Wire tutorial - https://github.com/google/wire/tree/main/_tutorial#making-changes-with-wire

  •  Tags:  
  • go
  • Related