Guest User

Untitled

a guest
Jun 22nd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. // Implementation of the 99 Bottles Of Beer song in Go.
  2. package main
  3.  
  4. import (
  5. "fmt"
  6. "os"
  7. "strconv"
  8. "strings"
  9. )
  10.  
  11. func main() {
  12. // Get argument for noOfBottles and convert to integer
  13. if len(os.Args) != 2 {
  14. fmt.Println("Usage: ./beer <no. of bottles>")
  15. fmt.Println("If a non-integer is passed, behaviour may be unexpected.")
  16. os.Exit(1)
  17. }
  18. noOfBottlesArg, _ := strconv.Atoi(os.Args[1])
  19.  
  20. doRhyme(noOfBottlesArg)
  21. }
  22.  
  23. // doRhyme outputs the rhyme with number of bottles specified in noOfBottles
  24. func doRhyme(noOfBottles int) {
  25. // Assign parts of the rhyme to variables
  26. part1 := "%d bottles of beer"
  27. part2 := " on the wall,"
  28. part3 := "Take one down, pass it around, "
  29. // Loop untill 2 bottles of beer on the wall
  30. for noOfBottles > 1 {
  31. // First line
  32. fmt.Printf(part1+part2+" "+part1+",\n", noOfBottles, noOfBottles)
  33. if noOfBottles == 2 {
  34. // Make part1 singular, so the second line of the
  35. // ...second last verse is correct
  36. part1 = strings.Replace(part1, "s", " ", -1)
  37. }
  38. // Second line
  39. fmt.Printf(part3+part1+part2+"\n", noOfBottles-1)
  40. noOfBottles--
  41. }
  42. // Last verses
  43. fmt.Println("1 bottle of beer on the wall, 1 bottle of beer, ")
  44. fmt.Println("Take one down, pass it around, no more bottles of beer on the wall!")
  45. fmt.Println("No more bottles of beer on the wall, no more bottles of beer,")
  46. fmt.Println("We've taken them down and passed them around; now we're drunk and passed out!")
  47. }
Add Comment
Please, Sign In to add comment