Golang 中国

zykzhang

Golang China 第 3100 号会员,加入于 2015-03-15 09:28



Go 小白,互联网新人

zykzhang最近回复了


回复了 zykzhang 创建的主题: go test坑 2016-10-10 11:14

出现这个问题的原因是dir_a dir_d都有TestMain(m *testing.M)

回复了 zykzhang 创建的主题: mac怎样操作能在局域网内访问linux-vm的服务? 2016-04-05 18:12

回复了 zykzhang 创建的主题: [求助]有人懂elasticSearch么? 希望有前辈踩过这个坑, 我技穷了 2015-12-17 09:54

这是mapping:

回复了 创建的主题: 2015-09-01 15:03

markdown 不好好用,排版让人很不想读,so,我偷了个懒~

回复了 zykzhang 创建的主题: 链接错误:undefined symbol: main.main exit status 127 2015-08-12 10:54

@synova 靠..本人已死,有事烧纸

回复了 zykzhang 创建的主题: 链接错误:undefined symbol: main.main exit status 127 2015-08-12 10:39

代码:

.
├── mlib
│   ├── manager.go
│   └── manager_test.go
├── mp
│   ├── mp3.go
│   ├── play.go
│   └── wav.go
└── mplayer.go

manager.go:

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, 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")
    }
    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) {
        return nil
    }
    removedMusic := &m.musics[index]
    m.musics = append(m.musics[:index], m.musics[index+1:]...)
    return removedMusic
}

mp3.go:

package mp

import (
    "fmt"
    "time"
)

type MP3Player struct {
    stat    int
    process int
}

func (p *MP3Player) Play(source string) {
    fmt.Println("Playing MP3 music", source)
    p.process = 0
    for p.process < 100 {
        time.Sleep(100 * time.Microsecond)
        fmt.Println(".")
        p.process += 10
    }
    fmt.Println("\nFinished playing", source)
}

play.go:

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)
    }

    P.Play(source)
}

mplayer.go:

package main

import (
    "bufio"
    "fmt"
    "os"
    "practice/smp/mlib"
    "practice/smp/mp"
    "strconv"
    "strings"
)

var lib *mlib.MusicManager
var id int = 1
var ctrl, siginal chan int

func handleLibCommands(tokens []string) {
    switch tokens[1] {
    case "list":
        for i := 0; i < lib.Len(); i++ {
            e, _ := lib.Get(i)
            fmt.Println(i+1, ":", e.Name, e.Artist, e.Source, e.Type)
        }
    case "add":
        if len(tokens) == 6 {
            id++
            lib.Add(&mlib.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 {
            name, _ := strconv.Atoi(tokens[2])
            lib.Remove(name)
        } else {
            fmt.Println("USAGE: lib remove<id>")
        }
    default:
        fmt.Println("Unrecognized command:", tokens[1])
    }
}
func handlePlayCommand(tokens []string) {
    if len(tokens) != 2 {
        fmt.Println("USAGE:lib paly<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)
}
func mian() {
    fmt.Println(`
        Enter following commands to control the player:
        lib list -- View the existing musicx lib
        lib add <name><artist><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
        `)

    lib = mlib.NewMusicManager()
    r := bufio.NewReader(os.Stdin)

    for {
        fmt.Println("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 commad: ", tokens[0])
        }
    }
}
回复了 zykzhang 创建的主题: Golang操作mysql创建表格的问题 2015-05-05 20:15

我把打印出来的SQL语句粘贴到terminal里可以成功执行的.

回复了 sunfmin 创建的主题: 互联网技术男和妹子微信群 2015-03-24 19:38

老兄,换个头像吧,要不哪会有妹子加

回复了 zykzhang 创建的主题: 哪位大神能给个Golang的学习路线? 2015-03-17 11:53

可是没有QQ...

zykzhang 创建的更多回复