example_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // CookieJar - A contestant's algorithm toolbox
  2. // Copyright (c) 2013 Peter Szilagyi. All rights reserved.
  3. //
  4. // CookieJar is dual licensed: use of this source code is governed by a BSD
  5. // license that can be found in the LICENSE file. Alternatively, the CookieJar
  6. // toolbox may be used in accordance with the terms and conditions contained
  7. // in a signed written agreement between you and the author(s).
  8. package set_test
  9. import (
  10. "fmt"
  11. "gopkg.in/karalabe/cookiejar.v2/collections/set"
  12. )
  13. // Insert some numbers into a set, remove one and sum the remainder.
  14. func Example_usage() {
  15. // Create a new set and insert some data
  16. s := set.New()
  17. s.Insert(3.14)
  18. s.Insert(1.41)
  19. s.Insert(2.71)
  20. s.Insert(10) // Isn't this one just ugly?
  21. // Remove unneeded data and verify that it's gone
  22. s.Remove(10)
  23. if !s.Exists(10) {
  24. fmt.Println("Yay, ugly 10 is no more!")
  25. } else {
  26. fmt.Println("Welcome To Facebook")
  27. }
  28. // Sum the remainder and output
  29. sum := 0.0
  30. s.Do(func(val interface{}) {
  31. sum += val.(float64)
  32. })
  33. fmt.Println("Sum:", sum)
  34. // Output:
  35. // Yay, ugly 10 is no more!
  36. // Sum: 7.26
  37. }