go語言編程書上有一些代碼有誤和遺漏的地方,這里就行了修改與加如了一小段代碼。
成都創(chuàng)新互聯(lián)公司是工信部頒發(fā)資質(zhì)IDC服務器商,為用戶提供優(yōu)質(zhì)的簡陽服務器托管服務
---開始,我也搜個百度,大多都是一樣的,而且在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] // 從數(shù)組切片中刪除元素 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) }
-----------------------------------------------------------------------------------------------------
如上面有所遺漏或代碼有誤,請留言。歡迎勘誤指正。
本文題目:go語言編程音樂庫代碼
文章URL:http://jinyejixie.com/article0/igohoo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供關鍵詞優(yōu)化、網(wǎng)站策劃、、網(wǎng)站維護、網(wǎng)站收錄、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)