Skip to content

Instantly share code, notes, and snippets.

@lesismal
Created May 17, 2023 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lesismal/0e6de663bc22a620c641832e348ecc60 to your computer and use it in GitHub Desktop.
Save lesismal/0e6de663bc22a620c641832e348ecc60 to your computer and use it in GitHub Desktop.
package main
type IDevice interface {
Fn()
Sync()
}
type DeviceFn interface {
Fn()
}
type PC struct{}
func (pc *PC) Fn() {
println("PC Device fn!!!")
}
type Router struct{}
func (router *Router) Fn() {
println("Router Device sync")
}
type Device struct {
DeviceFn
}
func (device *Device) Sync() {
println("Start Sync ...")
device.Fn()
println("Start Done.")
}
func NewDevice(d DeviceFn) IDevice {
return &Device{
DeviceFn: d,
}
}
func StartSync(d IDevice) {
d.Sync()
}
func main() {
p := NewDevice(&PC{})
r := NewDevice(&Router{})
StartSync(p)
StartSync(r)
}
@lesismal
Copy link
Author

output:

Start Sync ...
PC Device fn!!!
Start Done.
Start Sync ...
Router Device sync
Start Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment