Skip to content

Instantly share code, notes, and snippets.

@mkock
Created August 20, 2020 18:58
Show Gist options
  • Select an option

  • Save mkock/fbfeb00c4fad349ad8d8a041d4a09aa7 to your computer and use it in GitHub Desktop.

Select an option

Save mkock/fbfeb00c4fad349ad8d8a041d4a09aa7 to your computer and use it in GitHub Desktop.
Bus Ride, code snippet 04
// Bus carries Passengers from A to B if they have a valid bus ticket.
type Bus struct {
name string
passengers map[string]Passenger
}
// Add adds a single Passenger to the Bus. For brevity, we don't care too much about accidentally adding the same Passenger more than once.
func (b *Bus) Add(p Passenger) {
if b.passengers == nil {
b.passengers = make(map[string]Passenger)
}
b.passengers[p.SSN] = p
}
// VisitPassengers calls function visitor for each Passenger on the Bus.
func (b *Bus) VisitPassengers(visitor func(Passenger)) {
for _, p := range b.passengers {
visitor(p)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment