Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import "fmt"
- type State struct {
- X int
- }
- type Command interface {
- Execute(state *State)
- }
- type MoveLeft struct {
- Command
- }
- func (c *MoveLeft) Execute(state *State) {
- state.X -= 1
- }
- type Executor interface {
- Execute(command Command)
- }
- type Dummy struct {
- State State
- Executor
- }
- func (d *Dummy) Execute(command Command) {
- command.Execute(&d.State)
- }
- func main() {
- foo := Dummy{State: State{X: 0}}
- fmt.Println(foo.State) // {0}
- // Cannot use 'MoveLeft{}' (type MoveLeft) as the type CommandType does
- // not implement 'Command' as the 'Execute' method has a pointer reciever
- //foo.Execute(MoveLeft{})
- // но по ссылке все гуд
- foo.Execute(&MoveLeft{})
- fmt.Println(foo.State) // {-1}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement