pingo--util.go 源码阅读

时间:2024-12-27 09:37:44

package pingo

import (
    "fmt"
    "math/rand"
    "strings"
)

type meta string  //定义  自定义数据类型
//输出包含自身在内的参数字符串
func (h meta) output(key, val string) {
    fmt.Printf("%s: %s: %s\n", string(h), key, val)
}

func (h meta) parse(line string) (key, val string) {
    if line == "" {
        return
    }

    if len(line) < len(string(h)) {
        return
    }

    if line[0:len(string(h))] != string(h) {//比较当前字符串和参数字符串是否是以当前字符串开始的
        return
    }

    line = line[len(string(h))+2:] //当前字符串字符长度+2 截取字符串 到末尾
    end := strings.IndexByte(line, ':') //line中是否包含:字符
    if end < 0 {
        return
    }

    return line[0:end], line[end+2:]
}

var _letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-") //字rune符集合
//随机产生n个字符串返回  
func randstr(n int) string {
    b := make([]rune, n)
    l := len(_letters)

    for i := range b {
        b[i] = _letters[rand.Intn(l)]
    }

    return string(b)
}