Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. package main
  2.  
  3. import "fmt"
  4.  
  5. type ternary struct {
  6. conditionIsTrue bool
  7. trueValue interface{}
  8. }
  9.  
  10. func If(condition bool) *ternary {
  11. return &ternary{conditionIsTrue: condition}
  12. }
  13.  
  14. func (t *ternary) Then(trueValue interface{}) *ternary {
  15. if t.conditionIsTrue {
  16. t.trueValue = trueValue
  17. }
  18. return t
  19. }
  20.  
  21. func (t *ternary) Else(falseValue interface{}) interface{} {
  22. if t.conditionIsTrue {
  23. return t.trueValue
  24. } else {
  25. return falseValue
  26. }
  27. }
  28.  
  29. func main() {
  30. trueValue := If(true).Then("true value").Else("false value")
  31. fmt.Println("Result:", trueValue)
  32.  
  33. falseValue := If(false).Then("true value").Else("false value")
  34. fmt.Println("Result:", falseValue)
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement