example_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package feature_test
  2. import (
  3. "flag"
  4. "fmt"
  5. "go-common/library/exp/feature"
  6. )
  7. var (
  8. AStableFeature feature.Feature = "a-stable-feature"
  9. AStagingFeature feature.Feature = "a-staging-feature"
  10. )
  11. var exampleFeatures = map[feature.Feature]feature.Spec{
  12. AStableFeature: feature.Spec{Default: true},
  13. AStagingFeature: feature.Spec{Default: false},
  14. }
  15. func init() {
  16. feature.DefaultGate.Add(exampleFeatures)
  17. feature.DefaultGate.AddFlag(flag.CommandLine)
  18. }
  19. // This example create an example to using default features.
  20. func Example() {
  21. knows := feature.DefaultGate.KnownFeatures()
  22. fmt.Println(knows)
  23. enabled := feature.DefaultGate.Enabled(AStableFeature)
  24. fmt.Println(enabled)
  25. enabled = feature.DefaultGate.Enabled(AStagingFeature)
  26. fmt.Println(enabled)
  27. // Output: [a-stable-feature=true|false (default=true) a-staging-feature=true|false (default=false)]
  28. // true
  29. // false
  30. }
  31. // This example parsing flag from command line and enable a staging feature.
  32. func ExampleFeature() {
  33. knows := feature.DefaultGate.KnownFeatures()
  34. fmt.Println(knows)
  35. enabled := feature.DefaultGate.Enabled(AStagingFeature)
  36. fmt.Println(enabled)
  37. flag.Set("feature-gates", fmt.Sprintf("%s=true", AStagingFeature))
  38. enabled = feature.DefaultGate.Enabled(AStagingFeature)
  39. fmt.Println(enabled)
  40. // Output: [a-stable-feature=true|false (default=true) a-staging-feature=true|false (default=false)]
  41. // false
  42. // true
  43. }