package xy import ( "fmt" ) type Point struct { X int `json:"x"` Y int `json:"y"` } func (p Point) Go(dir Direction) (Point, error) { switch dir { case Up: return Point{X: p.X, Y: p.Y - 1}, nil case Down: return Point{X: p.X, Y: p.Y + 1}, nil case Left: return Point{X: p.X - 1, Y: p.Y}, nil case Right: return Point{X: p.X + 1, Y: p.Y}, nil } return p, fmt.Errorf("invalid direction: %s", dir) } type Direction string const ( Up Direction = "up" Down Direction = "down" Left Direction = "left" Right Direction = "right" ) func (d Direction) Validate() error { if d == Up || d == Down || d == Left || d == Right { return nil } return fmt.Errorf("invalid direction: %s (out of %v)", d, []Direction{Up, Down, Left, Right}) }