Skip to content

Instantly share code, notes, and snippets.

@caelifer
Last active October 20, 2016 11:34
Show Gist options
  • Save caelifer/0d865486515a8d9f0cea to your computer and use it in GitHub Desktop.
Save caelifer/0d865486515a8d9f0cea to your computer and use it in GitHub Desktop.
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"
}
@caelifer
Copy link
Author

caelifer commented Oct 27, 2015

Live code - https://play.golang.org/p/bgrThQvJBH
Output:

There are three hard problems in computer science: naming things and counting things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment