Last active
October 20, 2016 11:34
-
-
Save caelifer/0d865486515a8d9f0cea to your computer and use it in GitHub Desktop.
This file contains 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
package main | |
import ( | |
"fmt" | |
"strconv" | |
"strings" | |
) | |
type ProblemCounter int | |
type HardProblems int | |
const ( | |
naming HardProblems = 1 << iota | |
counting | |
) | |
func main() { | |
const hardProblems = naming | counting | |
fmt.Printf("There are %s hard problems in computer science: %s.", ProblemCounter(hardProblems), hardProblems) | |
} | |
func (n ProblemCounter) String() string { | |
if n == 3 { | |
return "three" | |
} | |
return strconv.Itoa(int(n)) | |
} | |
func (hp HardProblems) String() string { | |
res := make([]string, 0, 2) | |
if hp&naming != 0 { | |
res = append(res, "naming things") | |
} | |
if hp&counting != 0 { | |
res = append(res, "counting things") | |
} | |
if len(res) > 0 { | |
return strings.Join(res, " and ") | |
} | |
return "UNKNOWN" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Live code - https://play.golang.org/p/bgrThQvJBH
Output: