GO语言练习:第二个工程--模拟音乐播放器
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了GO语言练习:第二个工程--模拟音乐播放器,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7110字,纯文字阅读大概需要11分钟。
内容图文
![GO语言练习:第二个工程--模拟音乐播放器](/upload/InfoBanner/zyjiaocheng/1161/921fbd6b8b39491981a526ed7b428062.jpg)
1、代码
2、编译及运行
1、目录结构
1.1)
1 $ tree 2 . 3 ├── mplayer.go 4 └── src 5 ├── mlib 6 │ ├── manager.go 7 │ └── manager_test.go 8 └── mp 9 ├── mp3.go 10 └── play.go
1.2)mlib库的代码
1.2.1)manager.go代码
1 package library 2 3 import "errors" 4 5type MusicEntry struct { 6 Id string 7 Name string 8 Artist string 9 Genre string 10 Source string 11 Type string 12} 1314type MusicManager struct { 15 musics []MusicEntry 16} 1718 func NewMusicManager() * MusicManager { 19return &MusicManager {make( []MusicEntry, 0)} 20} 2122 func (m * MusicManager) Len() int { 23return len(m.musics); 24} 2526 func (m* MusicManager ) Get(index int) (music * MusicEntry, err error) { 27if index < 0 || index >= len(m.musics) { 28return nil, errors.New("Index out of range.") 29 } 3031return &m.musics[index], nil 32} 3334 func (m * MusicManager) Find (name string) * MusicEntry { 35if len(m.musics) == 0 { 36return nil 37 } 3839for _,m := range m.musics { 40if m.Name == name { 41return &m 42 } 43 } 44return nil 45} 4647 func (m * MusicManager) Add (music * MusicEntry) { 48 m.musics = append(m.musics, * music) 49} 5051 func (m * MusicManager) Remove (index int) * MusicEntry { 52if index < 0 || index >= len(m.musics) { 53return nil 54 } 55 removedMusic := &m.musics[index] 5657if index < len(m.musics) - 1 { 58 m.musics = append(m.musics[:index - 1], m.musics[index + 1:]...) 59 }elseif index == 0 { 60 m.musics = make([]MusicEntry, 0) 61 } else { 62 m.musics = m.musics[:index - 1] 63 } 6465return removedMusic 66} 6768 func (m * MusicManager) RemoveByName (name string) * MusicEntry { 69 var removedMusic * MusicEntry = nil 70 var iPos int = -1 71for i := 0; i < m.Len(); i++ { 72if m.musics[i].Name == name { 73 iPos = i 74break75 } 76 } 7778if iPos < 0 { 79return nil 80 } 8182 removedMusic = m.Remove(iPos) 8384return removedMusic 85 }
1.2.2)manager.go的测试代码manager_test.go
1 package library 2 3 import ( 4 "testing" 5) 6 7 func TestOps(t * testing.T) { 8 mm := NewMusicManager() 9if mm == nil { 10 t.Error("NewMusicManager faild."); 11 } 12if mm.Len() != 0 { 13 t.Error("NewMusicManager faild, not empty") 14 } 15 m0 := &MusicEntry { "1", "My Heart Will Go On", "Celion Dion", "Pop", "http://qbox.me/24501234", "MP3" } 16 mm.Add(m0) 1718if mm.Len() != 1 { 19 t.Error("MusicManager.Add faild.") 20 } 2122 m := mm.Find(m0.Name) 23if m == nil { 24 t.Error("MusicManager.Find faild") 25 } 2627if m.Id != m0.Id || 28 m.Name != m0.Name || 29 m.Artist != m0.Artist || 30 m.Genre != m0.Genre || 31 m.Source != m0.Source || 32 m.Type != m0.Type { 33 t.Error("MusicManager.Find() faild. Found item mismatch.") 34 } 3536 m, err := mm.Get(0) 37if m == nil { 38 t.Error("MusicManager.Get() faild.", err) 39 } 4041 m = mm.Remove(0) 42if m == nil || mm.Len() != 0 { 43 t.Error("MusicManager.Remove() faild.", err) 44 } 45 }
1.3)mp库代码
1.3.1)src/mp/mp3.go代码
1 package mp 2 3 import ( 4 "fmt" 5 "time" 6) 7 8type MP3Player struct { 9 stat int10 progress int11} 1213type WAVPlayer struct { 14 stat int15 progress int16} 1718 func (p * MP3Player) Play (source string) { 19 fmt.Println("Playing MP3 music", source) 2021 p.progress = 0 2223for p.progress < 100 { 24 time.Sleep(100 * time.Millisecond) 25 fmt.Print(".") 26 p.progress += 10 27 } 28 fmt.Println("\nFinished playing", source) 29} 3031 func (p * WAVPlayer) Play (source string) { 32 fmt.Println("Playing WAV music", source) 3334 p.progress = 0 3536for p.progress < 100 { 37 time.Sleep(100 * time.Millisecond) 38 fmt.Print(".") 39 p.progress += 10 40 } 41 fmt.Println("\nFinished playing", source) 42 }
1.3.2)src/mp/play.go代码
1 package mp 2 3 import "fmt" 4 5 type Player interface { 6 Play(source string) 7} 8 9func Play(source, mtype string) { 10 var p Player 1112switch mtype { 13case "MP3" : 14 p = &MP3Player{} 15case "WAV" : 16 p = &WAVPlayer{} 17default : 18 fmt.Println("Unsupported music type", mtype) 19return20 } 21 p.Play(source) 22 }
1.4)main package模块代码mplayer.go
1 package main 2 3 import ( 4 "bufio" 5 "fmt" 6 "os" 7 "strconv" 8 "strings" 910 "mlib" 11 "mp" 12) 1314 var lib * library.MusicManager 15 var id int = 1 16 var ctrl, signal chan int1718func handleLibCommands(tokens []string) { 19switch tokens[1] { 20case "list" : 21for i := 0; i < lib.Len(); i++ { 22 e, _ := lib.Get(i) 23 fmt.Println(i + 1, ":", e.Name, e.Artist, e.Source, e.Type) 24 } 25case "add" : 26if len(tokens) == 7 { 27 id++ 28 lib.Add(&library.MusicEntry { strconv.Itoa(id), tokens[2], tokens[3], tokens[4], tokens[5], tokens[6] }) 29 } else { 30 fmt.Println("USAGE : lib add <name><artist><genre><source><type> (7 argv)") 31 } 32case "remove" : 33if len(tokens) == 3 { 34 lib.RemoveByName(tokens[2]) 35 } else { 36 fmt.Println("USAGE: lib remove <name>") 37 } 38default : 39 fmt.Println("Unrecogized lib command: ", tokens[1]) 40 } 41} 4243func handlePlayCommands(tokens []string) { 44if len(tokens) != 2 { 45 fmt.Println("USAGE : play <name>") 46return47 } 4849 e := lib.Find(tokens[1]) 50if e == nil { 51 fmt.Println("The music", tokens[1], "does not exist.") 52return53 } 5455 mp.Play(e.Source, e.Type) 56} 5758func main() { 59 fmt.Println(` 60 Enter following commands to control the player: 61 lib list --View the existing music lib 62 lib add <name><artist><genre><source><type> -- Add a music to the music lib 63 lib remove <name> --Remove the specified music from the lib 64 play <name> -- Play the specified music 65 `) 66 lib = library.NewMusicManager() 6768 r := bufio.NewReader(os.Stdin) 6970for i := 0; i <= 100; i++ { 71 fmt.Print("Enter command-> ") 72 rawLine, _, _ := r.ReadLine() 7374 line := string(rawLine) 75if line == "q" || line == "e" { 76break77 } 78 tokens := strings.Split(line, " ") 7980if tokens[0] == "lib" { 81 handleLibCommands(tokens) 82 } elseif tokens[0] == "play" { 83 handlePlayCommands(tokens) 84 } else { 85 fmt.Println("Unrecognized command :", tokens[0]) 86 } 87 } 88 }
2)编译及运行
2.1)设置环境GOPATH
$ export GOPATH="/home/normal/musicplayer"
2.2)编译
1 $ go build
2.3)查看编译后的目录结构
1 . 2 ├── mplayer.go 3 ├── musicplayer 4 └── src 5 ├── mlib 6 │ ├── manager.go 7 │ └── manager_test.go 8 └── mp 9 ├── mp3.go 10 └── play.go
2.4)运行
$ ./musicplayer Enter following commands to control the player: lib list --View the existing music lib lib add <name><artist><genre><source><type> -- Add a music to the music lib lib remove <name> --Remove the specified music from the lib play <name> -- Play the specified music Enter command-> lib add a b c d e Enter command-> lib list 1 : a b d e Enter command-> play a Unsupported music type e Enter command-> lib remove a Enter command-> lib add a b c d e MP3 USAGE : lib add <name><artist><genre><source><type> (7 argv) Enter command-> lib add a b c d MP3 Enter command-> lib list 1 : a b d MP3 Enter command-> play a Playing MP3 music d .......... Finished playing d Enter command-> q
注:代码来源《Go语言编程》第三章,代码结构与原书有差别
原文:http://www.cnblogs.com/fengbohello/p/4642376.html
内容总结
以上是互联网集市为您收集整理的GO语言练习:第二个工程--模拟音乐播放器全部内容,希望文章能够帮你解决GO语言练习:第二个工程--模拟音乐播放器所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。