go 接口案例,音乐播放器

manager.go
//package main
package mlib
import "errors" 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,)}
} func (m *MusicManager) Len() int{
return len(m.musics)
} func (m *MusicManager ) Get(index int) (music *MusicEntry,err error){
if index < ||index >=len(m.musics){
return nil,errors.New("index out of range")
}
return &m.musics[index],nil
} func (m *MusicManager) Find(t_name string) *MusicEntry{
if len(m.musics) == {
return nil
}
for _,value := range m.musics{
if value.Name == t_name{
return &value
}
}
return nil
} func (m *MusicManager) Add(music *MusicEntry){
m.musics =append(m.musics,*music)
} func (m *MusicManager) Remove(index int) *MusicEntry{
if index< ||index >=len(m.musics){
return nil
} removeMusic := &m.musics[index] m.musics =append ( m.musics[:index], m.musics[index+:]...)
return removeMusic
}

type MusicEntry struct {
    Id string
    Name string
    Artist string
    Source string
    Type string
}

首先我们先设计一个结构体用来存储歌曲的结构,上面的musicentry就是,id代表唯一比那好,剩下的故名之意,type是便是类型.mp3.avw

我们接下来通过musicmanager以一种数组切片的方式进行管理

上面的各个函数的功能就是正常的add增减remove删除,get(index),获得第index首歌曲,len求长度,list列出所有的歌曲

mp3.go
package mp
import "fmt"
import "time" type MP3player struct{
stat int
pro int
} func (p *MP3player )Play(source string){ p.pro=;
fmt.Println("MPS PLAY IS BEGINING") for p.pro<= {
time.Sleep(* time.Millisecond)
fmt.Print(".")
p.pro+=
}
fmt.Println("player os ending ",source)
} play.go
package mp import "fmt" type Player interface{
Play(source string)
} func Play(source ,m_type string){
var p Player
switch m_type{
case "MP3":
p=&MP3player{} default :
fmt.Println("the m_tye is not exits")
return
}
p.Play(source)
}

type MP3player struct{
    stat int
    pro int
}

这里我们使用interface接口的形式,我们可以使用一个接口才处理多种类型的音乐,根据不同的类型的音乐,去实现不同的接口就可以了

case "MP3":
            p=&MP3player{}

像上面那样去赋值,下面通过p.play就可以调用了,至于paly,这里不能实现真正的paly,只不过是一种形式罢了

package main

import "fmt"
import "bufio"
import "os" import "smp/mlib"
import "strings"
import "strconv" import "smp/mp" var lib *mlib.MusicManager
var id int=
var ctrl,signal chan int
func set(tokens[]string){
switch tokens[]{
case "list":
for i:=; i<lib.Len();i++{
e,_ :=lib.Get(i)
fmt.Println(e.Name,e.Artist,e.Source)
}
case "add":
id++
if len(tokens)=={
id++
lib.Add(&mlib.MusicEntry{strconv.Itoa(id),tokens[],tokens[],tokens[],tokens[]})
}else{
fmt.Println("tokens number is error")
}
default :
fmt.Println("tokens[1] is error")
}
} func musicplay(tokens [] string){
e :=lib.Find(tokens[])
if e==nil{
fmt.Println("find failed")
return
}
mp.Play(e.Source,e.Type)
} func main(){
fmt.Println("***************************************************being*******************************************************")
lib =mlib.NewMusicManager() r :=bufio.NewReader(os.Stdin)
fmt.Println("forof")
for {
rawline,_,_ :=r.ReadLine() line :=string(rawline) if line == "q" ||line =="e"{
break
}
fmt.Println("wozhi")
fmt.Println(len(line))
fmt.Println(line)
tokens :=strings.Split(line ," ")
fmt.Println(tokens)
if tokens[] == "lib" {
set(tokens)
} else if tokens[] == "play"{
musicplay(tokens)
}else{
fmt.Println("this is any this stdin")
} } }

上面就是主控制流程,我们这里主要就有两个函数,一个是set主要的功能就是添加歌曲,另一个主要的功能就是播放,这两个功能都是调用前面的几个模块,我们通过import已经导入了

我们从终端输出,通过readline读取,并进行string强转,split就行分割,提取argv,之后我们就可以正常处理了

下面是这个小程序的整体结构

go 接口案例,音乐播放器

下面是运行的情况

go 接口案例,音乐播放器

上一篇:包装类型的比较,如:Integer,Long,Double


下一篇:2017年 JavaScript 框架回顾 -- React生态系统