go语言编程书上有一些代码有误和遗漏的地方,这里就行了修改与加如了一小段代码。
---开始,我也搜个百度,大多都是一样的,而且在remove代码块还是有些问题(不能是传name)。
好吧!!不多说了.下面展示所有的代码。
------------------------------------------------------------------------------------------------------
先贴入口文件.
mplayer.go
package聽main import聽( 聽聽聽聽"bufio" 聽聽聽聽"fmt" 聽聽聽聽"os" 聽聽聽聽"strconv" 聽聽聽聽"strings" 聽聽聽聽"mplayer/library"聽//这里是目录结构哦,别放做了,src下的mplayer目录下的library目录 聽聽聽聽"mplayer/mp"聽//src下的mplayer目录下的mp目录 ) func聽handleLibCommands(tokens聽[]string)聽{ 聽聽聽聽if聽len(tokens)聽<聽2聽{ 聽聽聽聽聽聽聽聽fmt.Println(`聽 聽聽聽聽聽聽Enter聽following聽commands聽to聽control聽the聽player:聽 聽聽聽聽聽聽lib聽list聽--聽View聽the聽existing聽music聽lib聽 聽聽聽聽聽聽lib聽add聽<name><artist><source><type>聽--聽Add聽a聽music聽to聽the聽music聽lib聽 聽聽聽聽聽聽lib聽remove聽序号聽--聽Remove聽the聽specified聽music聽from聽the聽lib聽 聽聽聽聽聽聽`) 聽聽聽聽聽聽聽聽return 聽聽聽聽} 聽聽聽聽switch聽tokens[1]聽{ 聽聽聽聽case聽"list": 聽聽聽聽聽聽聽聽fmt.Println("序号聽聽MP3_id聽聽聽聽名字聽聽聽聽聽聽聽聽作者聽聽聽聽聽聽聽聽聽聽路径聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽类型") 聽聽聽聽聽聽聽聽for聽i聽:=聽0;聽i聽<聽lib.Len();聽i++聽{ 聽聽聽聽聽聽聽聽聽聽聽聽e,聽_聽:=聽lib.Get(i) 聽聽聽聽聽聽聽聽聽聽聽聽fmt.Printf("%-4d聽聽%-8s聽聽%-10s聽聽%-12s聽聽%-20s聽聽聽聽聽聽聽聽聽聽聽%-5s\n",聽i+1,聽e.Id,聽e.Name,聽e.Artist,聽e.Source,聽e.Type) 聽聽聽聽聽聽聽聽聽聽聽聽//fmt.Println("聽",聽i+1,聽":",聽"聽",聽e.Id,聽"聽聽聽",聽e.Name,聽"聽聽聽聽聽",聽e.Artist,聽"聽聽聽",聽e.Source,聽"聽聽聽",聽e.Type) 聽聽聽聽聽聽聽聽} 聽聽聽聽case聽"add": 聽聽聽聽聽聽聽聽{ 聽聽聽聽聽聽聽聽聽聽聽聽if聽len(tokens)聽==聽6聽{ 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽id++ 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽lib.Add(&library.MusicEntry{strconv.Itoa(id), 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽tokens[2],聽tokens[3],聽tokens[4],聽tokens[5]}) 聽聽聽聽聽聽聽聽聽聽聽聽}聽else聽{ 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽fmt.Println("USAGE:聽lib聽add聽<name><artist><source><type>") 聽聽聽聽聽聽聽聽聽聽聽聽} 聽聽聽聽聽聽聽聽} 聽聽聽聽case聽"remove": 聽聽聽聽聽聽聽聽if聽len(tokens)聽==聽3聽{ 聽聽聽聽聽聽聽聽聽聽聽聽index,聽_聽:=聽strconv.Atoi(tokens[2]) 聽聽聽聽聽聽聽聽聽聽聽聽//fmt.Println(index) 聽聽聽聽聽聽聽聽聽聽聽聽lib.Remove(index) 聽聽聽聽聽聽聽聽聽聽聽聽fmt.Println("序号聽聽MP3_id聽聽聽聽名字聽聽聽聽聽聽聽聽作者聽聽聽聽聽聽聽聽聽聽路径聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽类型") 聽聽聽聽聽聽聽聽聽聽聽聽for聽i聽:=聽0;聽i聽<聽lib.Len();聽i++聽{ 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽e,聽_聽:=聽lib.Get(i) 聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽fmt.Printf("%-4d聽聽%-8s聽聽%-10s聽聽%-12s聽聽%-20s聽聽聽聽聽聽聽聽聽聽聽%-5s\n",聽i+1,聽e.Id,聽e.Name,聽e.Artist,聽e.Source,聽e.Type) 聽聽聽聽聽聽聽聽聽聽聽聽} 聽聽聽聽聽聽聽聽}聽else聽{ 聽聽聽聽聽聽聽聽聽聽聽聽fmt.Println("USAGE:聽lib聽remove聽<id>") 聽聽聽聽聽聽聽聽} 聽聽聽聽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聽exist.") 聽聽聽聽聽聽聽聽return 聽聽聽聽} 聽聽聽聽mp.Play(e.Source,聽e.Type) } var聽lib聽*library.MusicManager var聽id聽int聽=聽0 func聽main()聽{ 聽聽聽聽lib聽=聽library.NewMusicManager() 聽聽聽聽fmt.Println(`聽 聽聽聽聽聽聽Enter聽following聽commands聽to聽control聽the聽player:聽 聽聽聽聽聽聽lib聽list聽--聽View聽the聽existing聽music聽lib聽 聽聽聽聽聽聽lib聽add聽<name><artist><source><type>聽--聽Add聽a聽music聽to聽the聽music聽lib聽 聽聽聽聽聽聽lib聽remove聽<序号>聽--聽Remove聽the聽specified聽music聽from聽the聽lib聽 聽聽聽聽聽聽play聽<name>聽--聽Play聽the聽specified聽music聽 聽聽聽聽聽聽q聽|聽e聽聽--聽quit聽|聽exit聽 聽`) 聽聽聽聽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"聽{ 聽聽聽聽聽聽聽聽聽聽聽聽handlePlayCommand(tokens) 聽聽聽聽聽聽聽聽}聽else聽{ 聽聽聽聽聽聽聽聽聽聽聽聽fmt.Println("Unrecognized聽command:",聽tokens[0]) 聽聽聽聽聽聽聽聽} 聽聽聽聽} }
manager.go //在mplayer目录下的library目录下
package聽library import聽( 聽聽聽聽"errors" 聽聽聽聽"fmt" ) type聽MusicEntry聽struct聽{ 聽聽聽聽Id聽string 聽聽聽聽Name聽string 聽聽聽聽Artist聽string 聽聽聽聽Source聽string 聽聽聽聽Type聽string } type聽MusicManager聽struct聽{ 聽聽聽聽musics聽[]MusicEntry } func聽NewMusicManager()聽*MusicManager聽{ 聽聽聽聽return聽&MusicManager{make([]MusicEntry,聽0)} } func聽(m聽*MusicManager)聽Len()聽int聽{ 聽聽聽聽return聽len(m.musics) } func聽(m聽*MusicManager)聽Get(index聽int)聽(music聽*MusicEntry,聽err聽error)聽{ 聽聽聽聽if聽index聽<聽0聽||聽index聽>=聽len(m.musics)聽{ 聽聽聽聽聽聽聽聽return聽nil,聽errors.New("Index聽out聽of聽range.") 聽聽聽聽} 聽聽聽聽//fmt.Println(m) 聽聽聽聽return聽&m.musics[index],聽nil } func聽(m聽*MusicManager)聽Find(name聽string)聽*MusicEntry聽{ 聽聽聽聽if聽len(m.musics)聽==聽0聽{ 聽聽聽聽聽聽聽聽return聽nil 聽聽聽聽} 聽聽聽聽for聽_,聽m聽:=聽range聽m.musics聽{ 聽聽聽聽聽聽聽聽if聽m.Name聽==聽name聽{ 聽聽聽聽聽聽聽聽聽聽聽聽return聽&m 聽聽聽聽聽聽聽聽} 聽聽聽聽} 聽聽聽聽return聽nil } func聽(m聽*MusicManager)聽Add(music聽*MusicEntry)聽{ 聽聽聽聽m.musics聽=聽append(m.musics,聽*music) } func聽(m聽*MusicManager)聽Remove(index聽int)聽*MusicEntry聽{ 聽聽聽聽if聽index聽<聽0聽||聽index聽>聽len(m.musics)聽{ 聽聽聽聽聽聽聽聽fmt.Println("请重新选择删除的序号..") 聽聽聽聽聽聽聽聽return聽nil 聽聽聽聽} 聽聽聽聽removedMusic聽:=聽&m.musics[index-1] 聽聽聽聽//聽从数组切片中删除元素 聽聽聽聽if聽index聽<聽len(m.musics)聽{聽//聽中间元素 聽聽聽聽聽聽聽聽m.musics聽=聽append(m.musics[:index-1],聽m.musics[index:]...) 聽聽聽聽}聽else聽{聽//聽删除的是最后一个元素 聽聽聽聽聽聽聽聽//fmt.Println("删除最后一个") 聽聽聽聽聽聽聽聽m.musics聽=聽m.musics[:index-1] 聽聽聽聽} 聽聽聽聽return聽removedMusic }
mp3.go //mplayer 目录下的mp目录
package聽mp import聽( 聽聽聽聽"fmt" 聽聽聽聽"time" ) type聽MP3Player聽struct聽{ 聽聽聽聽stat聽int 聽聽聽聽progress聽int } type聽WAVPlayer聽struct聽{ 聽聽聽聽stat聽int 聽聽聽聽progress聽int } func聽(p聽*MP3Player)聽Play(source聽string)聽{ 聽聽聽聽fmt.Println("Playing聽MP3聽music",聽source) 聽聽聽聽p.progress聽=聽0 聽聽聽聽for聽p.progress聽<聽100聽{ 聽聽聽聽聽聽聽聽time.Sleep(100聽*聽time.Millisecond)聽//聽聽假装正在播放 聽聽聽聽聽聽聽聽fmt.Print(".") 聽聽聽聽聽聽聽聽p.progress聽+=聽10 聽聽聽聽} 聽聽聽聽fmt.Println("\nFinished聽playing",聽source) } func聽(p聽*WAVPlayer)聽Play(source聽string)聽{ 聽聽聽聽fmt.Println("Playing聽WAV聽music",聽source) 聽聽聽聽p.progress聽=聽0 聽聽聽聽for聽p.progress聽<聽100聽{ 聽聽聽聽聽聽聽聽time.Sleep(100聽*聽time.Millisecond)聽//聽聽假装正在播放 聽聽聽聽聽聽聽聽fmt.Print(".") 聽聽聽聽聽聽聽聽p.progress聽+=聽10 聽聽聽聽} 聽聽聽聽fmt.Println("\nFinished聽playing",聽source) }
play.go //mplayer目录下的mp目录下
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) }
-----------------------------------------------------------------------------------------------------
如上面有所遗漏或代码有误,请留言。欢迎勘误指正。
本文出自 “Goooood” 博客,请务必保留此出处http://goooood.blog.51cto.com/5060201/1736072
有疑问加站长微信联系(非本文作者)