ExampleSet_nestedComparable demonstrates using Set with nested comparable structs
()
| 335 | |
| 336 | // ExampleSet_nestedComparable demonstrates using Set with nested comparable structs |
| 337 | func ExampleSet_nestedComparable() { |
| 338 | // Define nested comparable types |
| 339 | type Coordinate struct { |
| 340 | Lat, Lon float64 |
| 341 | } |
| 342 | |
| 343 | type Location struct { |
| 344 | Name string |
| 345 | Coords Coordinate |
| 346 | } |
| 347 | |
| 348 | places := set.New[Location]() |
| 349 | |
| 350 | places.Add(Location{"Eiffel Tower", Coordinate{48.8584, 2.2945}}) |
| 351 | places.Add(Location{"Statue of Liberty", Coordinate{40.6892, -74.0445}}) |
| 352 | places.Add(Location{"Eiffel Tower", Coordinate{48.8584, 2.2945}}) // duplicate |
| 353 | |
| 354 | fmt.Println("Unique places:", len(places)) |
| 355 | |
| 356 | // Check if location exists |
| 357 | eiffel := Location{"Eiffel Tower", Coordinate{48.8584, 2.2945}} |
| 358 | fmt.Println("Has Eiffel Tower:", places.Contains(eiffel)) |
| 359 | |
| 360 | // Output: |
| 361 | // Unique places: 2 |
| 362 | // Has Eiffel Tower: true |
| 363 | } |