這是一個建立於
的文章,其中的資訊可能已經有所發展或是發生改變。
go語言編程書上有一些代碼有誤和遺漏的地方,這裡就行了修改與加如了一小段代碼。
---開始,我也搜個百度,大多都是一樣的,而且在remove代碼塊還是有些問題(不能是傳name)。
好吧!!不多說了.下面展示所有的代碼。
------------------------------------------------------------------------------------------------------
先貼入口檔案.
mplayer.go
package聽mainimport聽(聽聽聽聽"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.MusicManagervar聽id聽int聽=聽0func聽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聽libraryimport聽(聽聽聽聽"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聽mpimport聽(聽聽聽聽"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聽mpimport聽"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