Home  >  Article  >  Backend Development  >  Wire google Inject with multiple returns of provider function

Wire google Inject with multiple returns of provider function

WBOY
WBOYforward
2024-02-05 21:15:351056browse

将 google Inject 与提供者函数的多重返回连线

Question content

Following the example of googlewire, we can initialize the event structure in the following way

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)
}

We can initialize via line:

func main() {
    e := initializeevent()
    e.start()
}
    
func initializeevent() event {
    wire.build(newevent, newmessage)
    return event{}
}

Is there a way to make the init function return multiple values, but we only need one return value to inject, for example:

func newmessage() (message,error ){
    //tbd
}

or

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

Correct answer


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

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

EDIT: Your question (whether it is possible to return an error from the init function) will be answered in the next part of the wire tutorial - https://github.com/google/wire/ tree/main/_tutorial#making-changes -with line

The above is the detailed content of Wire google Inject with multiple returns of provider function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete