Skip to content

Instantly share code, notes, and snippets.

@TimPetricola
Created March 16, 2014 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TimPetricola/9591090 to your computer and use it in GitHub Desktop.
Save TimPetricola/9591090 to your computer and use it in GitHub Desktop.
A simple CracklePop (FizzBuzz) program in Go
package main
import (
"fmt"
"strconv"
)
const crackle, pop = "Crackle", "Pop"
func CracklePop(i int) (res string) {
switch {
case i % 5 == 0 && i % 3 == 0:
res = crackle + pop
case i % 5 == 0:
res = pop
case i % 3 == 0:
res = crackle
default:
res = strconv.Itoa(i)
}
return
}
func runCracklePop(i int, max int) {
for ; i <= max; i++ {
fmt.Println(CracklePop(i))
}
}
func main() {
runCracklePop(1, 100)
}
package main
import "testing"
func cracklePopTestCase(t *testing.T, in int, out string) {
if res := CracklePop(in); res != out {
t.Errorf("CracklePop(%v) = %s, want %s", in, res, out)
}
}
func TestCracklePop(t *testing.T) {
cracklePopTestCase(t, 1, "1")
cracklePopTestCase(t, 3, "Crackle")
cracklePopTestCase(t, 5, "Pop")
cracklePopTestCase(t, 15, "CracklePop")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment