Guest User

Untitled

a guest
Jun 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. // FizzBuzz implentation in Go
  2. package main
  3.  
  4. import (
  5. "fmt"
  6. "os"
  7. "strconv"
  8. )
  9.  
  10. func main() {
  11. // Get argument for noOfLines and convert to integer
  12. if len(os.Args) != 2 {
  13. fmt.Println("Usage: ./fizzBuzz <no. of lines>")
  14. fmt.Println("If a non-integer is passed, behaviour may be unexpected.")
  15. os.Exit(1)
  16. }
  17. noOfLinesArg, _ := strconv.Atoi(os.Args[1])
  18.  
  19. doFizzBuzz(noOfLinesArg)
  20. }
  21.  
  22. func doFizzBuzz(noOfLines int) {
  23. for i := 1; i < noOfLines+1; i++ {
  24. switch {
  25. case i%3 == 0 && i%5 == 0:
  26. fmt.Println("FizzBuzz")
  27. case i%3 == 0:
  28. fmt.Println("Fizz")
  29. case i%5 == 0:
  30. fmt.Println("Buzz")
  31. default:
  32. fmt.Printf("%d\n", i)
  33. }
  34. i++
  35. }
  36. }
Add Comment
Please, Sign In to add comment