Manager.go
Package Mlib Import "errors" type musicentry struct {Id string Name string Genre string Artist string Sourc E String type string} type Musicmanager struct {musics []musicentry} func Newmusicmanager () *musicmanager {Retu RN &musicmanager{make ([]musicentry, 0)}} func (M *musicmanager) len () int {return Len (m.musics)} func (M *MUSICM Anager) Get (index int) (music *musicentry, err error) {if index < 0 | | index >= len (m.musics) {return nil, Erro Rs.
New ("Index out of range.") } return &m.musics[index], nil} func (M *musicmanager) Find (name string) *musicentry {if Len (m.musics) = = 0 {r Eturn Nil} For _, M: = Range M.musics {if M.name = = Name {return &m}} return nil} func (M *musicman Ager) Add (music *musicentry) {m.musics = append (M.musics, *music)} func (M *musicmanager) Remove (index int) *musicentr Y {if index < 0 | | index >= len (m.musics) {return nil} removedmusic: = &m.musics[index] M.musiCS = Append (M.musics[:index], m.musics[index+1:] ...)
Return Removedmusic}
Manager_test.go
Package Mlib Import "Testing" func testops (t *testing. T) {mm: = Newmusicmanager () if mm = = Nil {t.error ("Newmusicmanager failed, not empty")} if mm.
Len ()! = 0 {t.error ("Newmusicmanager failed, not empty.") } M0: = &musicentry{"1", "My Heart would Go on", "Celion Dion", "Pop", "http://qbox.me/24501234", "MP3",} MM.A DD (M0) if mm.
Len ()! = 1 {t.error ("Musicmanager.add () failed.") } m: = mm. Find (M0.
Name) if m = = nil {t.error ("Musicmanager.find () failed.") } if M.id! = M0. Id | | M.artist! = M0.
Artist | | M.name! = M0. Name | | M.genre! = M0.
Genre | | M.source! = M0. Source | | M.type! = M0. Type {t.error ("Musicmanager.find () failed.
Found item mismatch. ")} M, err: = mm. Get (0) if m = = nil {t.error ("Musicmanager.find () failed.
Found item mismatch. ")} M, err = mm. Get (0) if m = = nil {t.error ("Musicmanager.get () failed.", err)} m = mm. Remove (0) if m = = Nil | | Mm.
Len ()! = 0 {t.error ("Musicmanager.remove () failed.", Err)}}
Mp3.go
Package MP
Import (
"FMT"
"Time"
)
type mp3player struct {
stat int
Progress int
}
Func (P *mp3player) Play (source string) {
FMT. Println ("Playing MP3 Music", source)
p.progress = 0 for
p.progress < {time
. Sleep (Time.millisecond)
FMT. Print (".")
P.progress + = Ten
}
fmt. Println ("\nfinished playing", source)
}
Play.go
Package MP
Import "FMT"
type Player interface {
Play (source string)
}
func Play (source, Mtype String) {
var p Player
switch mtype {case
' MP3 ':
p = &mp3player{} case
"WAV":
p = & ; wavplayer{}
Default:
FMT. Println ("Unsupported music type", Mtype)
return
}
p.play (source)
}
Mplayer.go
Package main import ("Bufio" "FMT" "OS" "StrConv" "Strings" "Mlib" "MP") Var Lib *mlib. Musicmanager var id int = 1 var ctrl, signal Chan int func handlelibcommands (tokens []string) {switch tokens[1] {case "List": For I: = 0; I < lib. Len (); i++ {E, _: = lib. Get (i) fmt. Println (I+1, ":", E.name, E.artist, E.source, E.type)} case "Add": {if len (tokens) = = 7 {id++ lib. ADD (&mlib. Musicentry{strconv. Itoa (ID), tokens[2], tokens[3], tokens[4], tokens[5], Tokens[6]})} else {fmt. Println ("Usage:lib add <name><artist><source><type>")}} Case "Remove": If Len (tokens) = = 3 {index, err: = StrConv. Atoi (tokens[2]) if err! = Nil {fmt. Println ("Usage:lib Remvoe <index>")} else {lib. Remove (Index)}} else {fmt. Println ("Usage:lib remove <index>")} default:fmt. Println ("Unrecognized lib command:", Tokens[1])}} func Handleplaycommand (tokens []string) {if LeN (tokens)! = 2 {fmt. Println ("Usage:play <name>") return} e: = lib. Find (Tokens[1]) if E = = nil {fmt.
Println ("The Music", tokens[1], "does not exit.") } MP. Play (E.source, E.type)} func main () {FMT. Println (' Enter folloing commands to control the Player:lib list--view the existing music Lib Lib add <name& Gt;<artist><source><type>--add a music to the music Lib Lib remove <name>-Remove the SPECIF IED music from the Lib play <name>-play The specified music ') lib = mlib. Newmusicmanager () r: = Bufio. Newreader (OS. Stdin) for {fmt. Print ("Enter command->") Rawline, _, _: = R.readline () Line: = String (rawline) If line = = "Q" | | line = = ' E ' {break} tokens: = Strings. Split (line, "") if tokens[0] = = "Lib" {Handlelibcommands (tokens)} else if tokens[0] = = "Play" {Handleplayco Mmand (Tokens)} else {fmt.
Println ("Unrecognized command:", Tokens[0])}}