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
| func main() { | |
| expressLine := busservice.Bus{"Express Line"} | |
| expressLine.Passengers := make([]busservice.Passenger, 0) | |
| expressLine.Passengers = append(expressLine.Passengers, busservice.Passenger{SSN: "001"}) | |
| expressLine.Passengers = append(expressLine.Passengers, busservice.Passenger{SSN: "002"}) | |
| // Get a manifest! | |
| ssns := make([]string, 0) | |
| for _, p := range expressLine.Passengers { | |
| ssns = append(ssns, p.SSN) |
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 // Map of SSN to Passengers | |
| } |
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) |
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
| func main() { | |
| expressLine := busservice.Bus{"Express Line"} | |
| expressLine.Add(busservice.Passenger{"001"}) | |
| expressLine.Add(busservice.Passenger{"002"}) | |
| // Get a manifest! | |
| ssns := make([]string, 0) | |
| expressLine.VisitPassengers(func(p busservice.Passenger) { ssns = append(ssns, p.SSN) }) | |
| fmt.Printf("This bus carries %d passengers, here are their SSN's: %v\n", len(ssns), ssns) | |
| } |
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
| // FindPassenger returns the Passenger that matches the given SSN, if found. Otherwise, an empty Passenger is returned. | |
| func (b *Bus) FindPassenger(ssn string) Passenger { | |
| if p, ok := b.passengers[ssn]; ok { | |
| return p | |
| } | |
| return Passenger{} // A nobody. | |
| } |
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
| // UpdatePassengers calls function visitor for each Passenger on the bus. Passengers are passed by reference and may be modified. | |
| func (b *Bus) UpdatePassengers(visitor func(*Passenger)) { | |
| ps := make(map[string]Passenger, len(b.passengers)) | |
| for ssn, p := range b.passengers { | |
| visitor(&p) | |
| ps[ssn] = p | |
| } | |
| b.passengers = ps | |
| } |
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
| func main() { | |
| // Previous statements... | |
| // Assign seat numbers to passengers. | |
| var seatNumber uint8 | |
| expressLine.UpdatePassengers(func(p *busservice.Passenger) { | |
| seatNumber++ | |
| p.SeatNumber = seatNumber | |
| }) |
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
| // Passengers represents a set of Passengers, using their SSN as key. | |
| type Passengers map[string]Passenger | |
| // NewPassengerSet returns an empty set of Passengers, ready to use. | |
| func NewPassengerSet() Passengers { | |
| return make(map[string]Passenger) | |
| } | |
| // Add adds a Passenger to Passengers. The Passenger will be overwritten if exists. | |
| func (p Passengers) Add(p Passenger) { |
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 Passengers | |
| } | |
| // NewBus returns a new Bus with an empty passenger set. | |
| func NewBus(name string) Bus { | |
| b := Bus{} | |
| b.name = name |
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
| func main() { | |
| expressLine := busservice.NewBus() | |
| expressLine.Add(busservice.Passenger{SSN: "001"}) | |
| expressLine.Add(busservice.Passenger{SSN: "002"}) | |
| // Get a manifest! | |
| ssns := expressLine.Manifest() | |
| fmt.Printf("This bus carries %d passengers, here are their SSN's: %v\n", len(ssns), ssns) | |
| } |