go语言编程音乐库代码

前端之家收集整理的这篇文章主要介绍了go语言编程音乐库代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

go语言编程书上有一些代码有误和遗漏的地方,这里就行了修改与加如了一小段代码

---开始,我也搜个百度,大多都是一样的,而且在remove代码块还是有些问题(不能是传name)。


好吧!!不多说了.下面展示所有的代码

------------------------------------------------------------------------------------------------------

先贴入口文件.

mplayer.go

packagemain

import(
"bufio"

"fmt"

"os"

"strconv"

"strings"

"mplayer/library"//这里是目录结构哦,别放做了,src下的mplayer目录下的library目录

"mplayer/mp"//src下的mplayer目录下的mp目录
)


funchandleLibCommands(tokens[]string){

iflen(tokens)<2{

fmt.Println(`

Enterfollowingcommandstocontroltheplayer:

liblist--Viewtheexistingmusiclib

libadd<name><artist><source><type>--Addamusictothemusiclib

libremove序号--Removethespecifiedmusicfromthelib

`)

return

}

switchtokens[1]{

case"list":
fmt.Println("序号MP3_id名字作者路径类型")
fori:=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("",":","",e.Type)

}

case"add":

{

iflen(tokens)==6{

id++

lib.Add(&library.MusicEntry{strconv.Itoa(id),tokens[2],tokens[3],tokens[4],tokens[5]})

}else{

fmt.Println("USAGE:libadd<name><artist><source><type>")

}

}

case"remove":

iflen(tokens)==3{

index,_:=strconv.Atoi(tokens[2])
//fmt.Println(index)
lib.Remove(index)
fmt.Println("序号MP3_id名字作者路径类型")
fori:=0;i<lib.Len();i++{

e,_:=lib.Get(i)

fmt.Printf("%-4d%-8s%-10s%-12s%-20s%-5s\n",e.Type)

}

}else{

fmt.Println("USAGE:libremove<id>")

}

default:

fmt.Println("Unrecognizedlibcommand:",tokens[1])

}

}

funchandlePlayCommand(tokens[]string){

iflen(tokens)!=2{

fmt.Println("USAGE:play<name>")

return

}

e:=lib.Find(tokens[1])

ife==nil{

fmt.Println("Themusic",tokens[1],"doesnotexist.")

return

}

mp.Play(e.Source,e.Type)

}

varlib*library.MusicManager

varidint=0

funcmain(){

lib=library.NewMusicManager()
fmt.Println(`

Enterfollowingcommandstocontroltheplayer:

liblist--Viewtheexistingmusiclib

libadd<name><artist><source><type>--Addamusictothemusiclib

libremove<序号>--Removethespecifiedmusicfromthelib

play<name>--Playthespecifiedmusic

q|e--quit|exit

`)

r:=bufio.NewReader(os.Stdin)

for{

fmt.Print("Entercommand->")

rawLine,_,_:=r.ReadLine()

line:=string(rawLine)

ifline=="q"||line=="e"{

break

}

tokens:=strings.Split(line,"")

iftokens[0]=="lib"{

handleLibCommands(tokens)

}elseiftokens[0]=="play"{

handlePlayCommand(tokens)

}else{

fmt.Println("Unrecognizedcommand:",tokens[0])

}

}

}


manager.go //在mplayer目录下的library目录下

packagelibrary

import(
"errors"
"fmt"
)

typeMusicEntrystruct{
Idstring

Namestring

Artiststring

Sourcestring

Typestring
}

typeMusicManagerstruct{
musics[]MusicEntry
}

funcNewMusicManager()*MusicManager{

return&MusicManager{make([]MusicEntry,0)}

}

func(m*MusicManager)Len()int{

returnlen(m.musics)

}

func(m*MusicManager)Get(indexint)(music*MusicEntry,errerror){

ifindex<0||index>=len(m.musics){

returnnil,errors.New("Indexoutofrange.")

}
//fmt.Println(m)
return&m.musics[index],nil

}

func(m*MusicManager)Find(namestring)*MusicEntry{

iflen(m.musics)==0{

returnnil

}

for_,m:=rangem.musics{

ifm.Name==name{

return&m

}

}

returnnil

}

func(m*MusicManager)Add(music*MusicEntry){

m.musics=append(m.musics,*music)

}

func(m*MusicManager)Remove(indexint)*MusicEntry{

ifindex<0||index>len(m.musics){
fmt.Println("请重新选择删除的序号..")
returnnil

}

removedMusic:=&m.musics[index-1]

//从数组切片中删除元素

ifindex<len(m.musics){//中间元素
m.musics=append(m.musics[:index-1],m.musics[index:]...)
}else{//删除的是最后一个元素
//fmt.Println("删除最后一个")
m.musics=m.musics[:index-1]

}

returnremovedMusic

}

mp3.go //mplayer 目录下的mp目录

packagemp

import(
"fmt"

"time"
)

typeMP3Playerstruct{
statint

progressint
}

typeWAVPlayerstruct{
statint

progressint
}

func(p*MP3Player)Play(sourcestring){

fmt.Println("PlayingMP3music",source)

p.progress=0

forp.progress<100{

time.Sleep(100*time.Millisecond)//假装正在播放

fmt.Print(".")

p.progress+=10

}

fmt.Println("\nFinishedplaying",source)

}

func(p*WAVPlayer)Play(sourcestring){

fmt.Println("PlayingWAVmusic",source)

}


play.go //mplayer目录下的mp目录下

packagemp

import"fmt"

typePlayerinterface{
Play(sourcestring)
}

funcPlay(source,mtypestring){

varpPlayer

switchmtype{

case"MP3":

p=&MP3Player{}

case"WAV":

p=&WAVPlayer{}

default:

fmt.Println("Unsupportedmusictype",mtype)

return

}

p.Play(source)

}



-----------------------------------------------------------------------------------------------------

如上面有所遗漏或代码有误,请留言。欢迎勘误指正。

猜你在找的Go相关文章