Created
August 20, 2020 18:58
-
-
Save mkock/fbfeb00c4fad349ad8d8a041d4a09aa7 to your computer and use it in GitHub Desktop.
Bus Ride, code snippet 04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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