Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // FizzBuzz implentation in Go
- package main
- import (
- "fmt"
- "os"
- "strconv"
- )
- func main() {
- // Get argument for noOfLines and convert to integer
- if len(os.Args) != 2 {
- fmt.Println("Usage: ./fizzBuzz <no. of lines>")
- fmt.Println("If a non-integer is passed, behaviour may be unexpected.")
- os.Exit(1)
- }
- noOfLinesArg, _ := strconv.Atoi(os.Args[1])
- doFizzBuzz(noOfLinesArg)
- }
- func doFizzBuzz(noOfLines int) {
- for i := 1; i < noOfLines+1; i++ {
- switch {
- case i%3 == 0 && i%5 == 0:
- fmt.Println("FizzBuzz")
- case i%3 == 0:
- fmt.Println("Fizz")
- case i%5 == 0:
- fmt.Println("Buzz")
- default:
- fmt.Printf("%d\n", i)
- }
- i++
- }
- }
Add Comment
Please, Sign In to add comment