There are some code errors and omissions in the Go Language programming book, and here is a small piece of code that is modified and added.
---start, I also search a Baidu, mostly are the same, and in the Remove code block still have some problems (cannot be passed name).
All right!! Not much to say. All the code is shown below.
------------------------------------------------------------------------------------------------------
Paste the entry file first.
Mplayer.go
package mainimport ( "Bufio" "FMT" "OS" "StrConv" "strings" "mplayer/ Library " //Here is the directory structure Oh, don't do it, SRC under the MPlayer directory of the Library directory " MPLAYER/MP " // The MP directory under the MPlayer directory under SRC) 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&nbSP; serial number -- Remove the specified music from the lib ') return } switch tokens[1] { case "List": fmt. PRINTLN ("Serial number MP3_id name author Paths Type ") 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), &NBSP;&NBSP;&NBSP;&NBSP;TOKENS[2],&NBSP;TOKENS[3],&NBSP;TOKENS[4],&NBSP;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) &nbsP; lib. Remove (index) fmt. PRINTLN ("Serial number MP3_id name author Paths Type ") 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 { &nbsP; 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 < Serial Number > -- 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, " ") &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;IF&Nbsp;tokens[0] == "Lib" { handlelibcommands (Tokens) } else if tokens [0] == "Play" { Handleplaycommand (Tokens) } else { fmt. Println ("Unrecognized command:", tokens[0]) } }}
Manager.go//In the Library directory under the MPlayer directory
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 ("Please re-select the deleted sequence number ...") return nil } removedMusic := &m.musics[index-1] // Delete an element from an array slice if index < len (m.musics) { // intermediate elements m.musics = append (M.musics[:index-1], m.musics[index:] ...) } else { // Delete the last element //fmt. Println ("Delete Last") m.musics = m.musics[:index-1] } return removedmusic}
Mp3.go The MP directory under the//mplayer directory
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) // pretend to be playing fmt. Print (".") p.progress += 10 } fmt. Println ("\nfinished playing", source)}func (p *wavplayer) play (source string) &NBsp { fmt. Println ("Playing wav music", source) p.progress = 0 for p.progress < 100 { time. Sleep (100 * time.millisecond) // pretend to be playing fmt. Print (".") p.progress += 10 } fmt. Println ("\nfinished playing", source)}
Play.go under the MP directory in the//mplayer directory
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:FM T.println ("Unsupported music type", Mtype) return} p.play (source)}
-----------------------------------------------------------------------------------------------------
Please leave a message if there is any omission or code error. You are welcome to correct the corrections.
This article is from the "Goooood" blog, make sure to keep this source http://goooood.blog.51cto.com/5060201/1736072
Go Language programming Music library code