In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to install and use go wire". In daily operation, I believe many people have doubts about how to install and use go wire. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to install and use go wire". Next, please follow the editor to study!
HelloWorld
Install wire
Go get-u github.com/google/wire/cmd/wirepackage mainimport "fmt" type Monster struct {Name string} func NewMonster (name string) Monster {return Monster {Name: name}} type Fighter struct {Name string} func NewFighter (name string) Fighter {return Fighter {Name: name}} type Mission struct {Fighter Fighter Monster Monster} func NewMission (p Fighter, m Monster) Mission {return Mission {Fighter: P, Monster: M } func (m Mission) start () {fmt.Printf ("% s defeates% s, world peace\ n", m.Fighter.Name, m.Monster.Name)} func main () {monster: = NewMonster ("kitty") fighter: = NewFighter ("Ultraman") mission: = NewMission (fighter, monster) mission.start ()} concept
Wire has two basic concepts, Provider (constructor) and Injector (injector).
Provider
Provider is actually creating a function.
Injector
The InitMission above us is Injector. Each injector is actually an object creation and initialization function. In this function, we just need to tell wire what type of object to create, this type of dependency, wire tool will generate a function for us to complete the creation and initialization of the object.
Create a wire to declare dependencies
Wireinject
Both wire.go and wire_gen.go files have a + build in the header, but one is followed by wireinject and the other is! wireinject. + build
It is actually a feature of the Go language. Conditional compilation, similar to CAccord +, allows you to pass in options when executing go build to determine whether certain files are compiled or not. The wire tool will only deal with files with wireinject, so we need to add this to our wire.go files. The generated wire_gen.go is for us to use, and wire does not need to be processed, so there is! wireinject.
Since there are two files now, we can't run the program with go run main.go, we can use go run. Run. Note that if you have an InitMission redefinition at run time, check to see if there is a blank line between your / / + build wireinject and package main lines, which must have
Execute the wire command and generate a wire_gen.go file
At this point, the study on "how to install and use go wire" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.