Skip to content

Commit

Permalink
feat: di with wire
Browse files Browse the repository at this point in the history
  • Loading branch information
Leizhenpeng committed Feb 20, 2023
1 parent 3f6c387 commit f1fac84
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 2 deletions.
57 changes: 57 additions & 0 deletions 18-di-with-wire/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import "fmt"

type Meta struct{}
type Message struct {
msg string
r Meta
}
type Mouth struct{ Message Message }
type Hand struct{ Message Message }
type People struct {
Mouth Mouth
Hand Hand
//..
}

func main_() {
meta := NewMeta()
message := NewMessage("A Hi To U", meta)
aMouth := NewMouth(message)
aHand := NewHand(message)
people := NewPeople(aMouth, aHand)
people.Hi()
}

//use wire
func main() {
p := InitAPeople("Nice to meet you!")
p.Hi()
}
func NewMeta() Meta {
return Meta{}
}
func NewMessage(msg string, r Meta) Message {
return Message{
msg: msg,
r: r,
}
}
func NewMouth(m Message) Mouth {
return Mouth{Message: m}
}
func NewHand(m Message) Hand {
return Hand{Message: m}
}
func NewPeople(g Mouth, t Hand) People {
return People{Mouth: g, Hand: t}
}

func (p People) Hi() {
msg := p.Mouth.Greet()
fmt.Println(msg)
}
func (g Mouth) Greet() string {
return g.Message.msg
}
17 changes: 17 additions & 0 deletions 18-di-with-wire/wire.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build wireinject
// +build wireinject

package main

import "github.com/google/wire"

func InitAPeople(msg string) People {
wire.Build(
NewPeople,
NewMouth,
NewHand,
//..
NewMessage,
NewMeta)
return People{}
}
18 changes: 18 additions & 0 deletions 18-di-with-wire/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ module 222

go 1.18

require github.com/stretchr/testify v1.8.1
require (
github.com/google/wire v0.5.0
github.com/stretchr/testify v1.8.1
)

require github.com/go-playground/assert v1.2.1

require github.com/go-playground/assert v1.2.1 // indirect
require (
github.com/google/subcommands v1.0.1 // indirect
golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b // indirect
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ github.com/go-playground/assert v1.2.1 h1:ad06XqC+TOv0nJWnbULSlh3ehp5uLuQEojZY5T
github.com/go-playground/assert v1.2.1/go.mod h1:Lgy+k19nOB/wQG/fVSQ7rra5qYugmytMQqvQ2dgjWn8=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/subcommands v1.0.1 h1:/eqq+otEXm5vhfBrbREPCSVQbvofip6kIz+mX5TUH7k=
github.com/google/subcommands v1.0.1/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/wire v0.5.0 h1:I7ELFeVBr3yfPIcc8+MWvrjk+3VjbcSzoXm3JVa+jD8=
github.com/google/wire v0.5.0/go.mod h1:ngWDr9Qvq3yZA10YrxfyGELY/AFWGVpy9c1LTRi1EoU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -14,6 +19,13 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b h1:NVD8gBK33xpdqCaZVVtd6OFJp+3dxkXuz7+U7KaVN6s=
golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down

0 comments on commit f1fac84

Please sign in to comment.