Created
March 16, 2014 22:57
-
-
Save TimPetricola/9591090 to your computer and use it in GitHub Desktop.
A simple CracklePop (FizzBuzz) program in Go
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" | |
) | |
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) | |
} |
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 "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