i have the starts of a 99bottles song, uses some pretty simple
channels and
a function that returns two values, for showing off some of the go
idioms
also plan on making it "slur" the lyrics progressively worse as more
beer
becomes consumed.
just need to find the time to be bothered coding it.
(swap random, non-space, char a with random, non-space. unequal-to-a,
char b)
/**
*Ninety-Nine Bottles Song Example
*Author: Bryan Knowles
*Attempts to show Go programming
*idioms while putting a twist
*to the traditional output.
**/
package main
import "fmt"
//Slurs (randomly swaps two chars) string s based on amount a
func slur(s string, a int) string {
/*TODO: "SLUR" THE STRING S BASED ON AMOUNT A*/
return s
}
//Channels the lyrics of the ninety-nine bottles song.
func song(ct int) (chan string, chan bool) {
//Creates channels for piping the song to the main
ch := make(chan string);
qt := make(chan bool);
go func(){
for i := ct; i >= 0; i-- {
//Assigns grammatical fixes
s, n := func ()(string, string){
if i == 1 {return "", ""}
if i == 0 {return "s", "n"}
return "s", ""
}();
//Pushes a true through to keep the main loop running.
qt<-true; ch<-slur(fmt.Sprintf("%v bottle%v of beer on the
wall,", i, s), i);
qt<-true; ch<-slur(fmt.Sprintf("%v bottle%v of beer.", i,
s), i);
qt<-true; ch<-slur(fmt.Sprintf("Take %vone down, pass
%vone around,", n, n), i);
qt<-true; ch<-slur(fmt.Sprintf("%v bottle%v of beer on the
wall.", i, s), i);
qt<-true; ch<-""
}
//Pushes a false through to kill the main loop.
qt<-false
}();
return ch, qt
}
func main(){for line, quit := song(9999); <-quit; fmt.Printf("%v\n", <-
line){}}
Just what I threw together in a half hour. The goroutines are kinda
pointless, since only one is running at a time, but they can only
compute their results after # of bottles has changed :P So,
technically, I could have made them just return their closures and
called those... but I wanted to create the impression of a bunch of
guys in a bar waiting to sing their part.
http://99-bottles-of-beer.net/language-limbo-1892.html
one could do a similar thing in go, but it would
be nice to include some interface stuff too.
maybe there could be an interface:
type Bottle interface {
BeerLeft() float
Drink(amount float)
}
then some beer drinkers could care what kind of
beer it was:
if b, ok := (<-passitaround).(*Budweiser); ok {
// nope, won't have any of that.
passitaround <- b
}
From the header comment:
* Create a shelf containing numbered beer bottles.
* Start tossing out numbers in descending order.
* If a bottle catches a number other than its own, it tosses it back.
* When a bottle catches its own number, it sings, empties itself, then dies.
*
* This is done like a carnival ring-toss, where the number is
* on the ring, and it is checked against the bottle it lands on.
* The bottle gets opened only when the numbers match.
-BobC
I would love that we "voted" on the best song, and
elect the author to submit it to 99-bottles
But I fear the confusion with the existing
language "Go!"
I've even seen a discussion in the issues
started by the creator of Go! over a
suggested name change for Google's Go.
So should we submit it, when we do,
with the name of "Google's Go" ?
Or possibly "Golang" (after the url) ?
Wow, many songs in a rather short time ;D
I would love that we "voted" on the best song, and
elect the author to submit it to 99-bottles
But I fear the confusion with the existing
language "Go!"
I've even seen a discussion in the issues
started by the creator of Go! over a
suggested name change for Google's Go.
So should we submit it, when we do,
with the name of "Google's Go" ?
Or possibly "Golang" (after the url) ?
I'm done with it. Really. Take me out back and shoot me now.
-BobC