Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Implementation of the 99 Bottles Of Beer song in Go.
- package main
- import (
- "fmt"
- "os"
- "strconv"
- "strings"
- )
- func main() {
- // Get argument for noOfBottles and convert to integer
- if len(os.Args) != 2 {
- fmt.Println("Usage: ./beer <no. of bottles>")
- fmt.Println("If a non-integer is passed, behaviour may be unexpected.")
- os.Exit(1)
- }
- noOfBottlesArg, _ := strconv.Atoi(os.Args[1])
- doRhyme(noOfBottlesArg)
- }
- // doRhyme outputs the rhyme with number of bottles specified in noOfBottles
- func doRhyme(noOfBottles int) {
- // Assign parts of the rhyme to variables
- part1 := "%d bottles of beer"
- part2 := " on the wall,"
- part3 := "Take one down, pass it around, "
- // Loop untill 2 bottles of beer on the wall
- for noOfBottles > 1 {
- // First line
- fmt.Printf(part1+part2+" "+part1+",\n", noOfBottles, noOfBottles)
- if noOfBottles == 2 {
- // Make part1 singular, so the second line of the
- // ...second last verse is correct
- part1 = strings.Replace(part1, "s", " ", -1)
- }
- // Second line
- fmt.Printf(part3+part1+part2+"\n", noOfBottles-1)
- noOfBottles--
- }
- // Last verses
- fmt.Println("1 bottle of beer on the wall, 1 bottle of beer, ")
- fmt.Println("Take one down, pass it around, no more bottles of beer on the wall!")
- fmt.Println("No more bottles of beer on the wall, no more bottles of beer,")
- fmt.Println("We've taken them down and passed them around; now we're drunk and passed out!")
- }
Add Comment
Please, Sign In to add comment