Wire google Inject with multiple returns of provider function

WBOY
Release: 2024-02-05 21:15:35
forward
1101 people have browsed it

将 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    
}
Copy after login

event.go

func newevent(g message ) event {
    return event{message : g}
}

type event struct {
    message message
}

func (e event) start() {
   fmt.println(msg)
}
Copy after login

We can initialize via line:

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

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
}
Copy after login

or

func NewMessage() (Message,Greeter) {
    //TBD
}
Copy after login


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
}
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template