本文整理汇总了Golang中bytes.SplitN函数的典型用法代码示例。如果您正苦于以下问题:Golang SplitN函数的具体用法?Golang SplitN怎么用?Golang SplitN使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SplitN函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: parseIndex
func parseIndex(sql []byte) (indexName string, indexId interface{}, userId uint64) {
var err error
keyspaceIndex := bytes.Index(sql, KEYSPACE_ID_COMMENT)
if keyspaceIndex == -1 {
panic(NewBinlogParseError(fmt.Sprintf("Error parsing index comment, doesn't contain keyspace id %v", string(sql))))
}
keyspaceIdComment := sql[keyspaceIndex+len(KEYSPACE_ID_COMMENT):]
indexCommentStart := bytes.Index(keyspaceIdComment, INDEX_COMMENT)
if indexCommentStart != -1 {
indexCommentParts := bytes.SplitN(keyspaceIdComment[indexCommentStart:], COLON_BYTE, 2)
userId, err = strconv.ParseUint(string(bytes.SplitN(indexCommentParts[1], mysqlctl.SPACE, 2)[0]), 10, 64)
if err != nil {
panic(NewBinlogParseError(fmt.Sprintf("Error converting user_id %v", string(sql))))
}
indexNameId := bytes.Split(indexCommentParts[0], DOT_BYTE)
indexName = string(indexNameId[1])
if indexName == "username" {
indexId = string(bytes.TrimRight(indexNameId[2], COLON))
} else {
indexId, err = strconv.ParseUint(string(bytes.TrimRight(indexNameId[2], COLON)), 10, 64)
if err != nil {
panic(NewBinlogParseError(fmt.Sprintf("Error converting index id %v %v", string(bytes.TrimRight(indexNameId[2], COLON)), string(sql))))
}
}
}
return
}
示例2: TestNew
func TestNew(t *testing.T) {
err := New("foo")
if err.Error() != "foo" {
t.Errorf("Wrong message")
}
err = New(fmt.Errorf("foo"))
if err.Error() != "foo" {
t.Errorf("Wrong message")
}
bs := [][]byte{New("foo").Stack(), debug.Stack()}
// Ignore the first line (as it contains the PC of the .Stack() call)
bs[0] = bytes.SplitN(bs[0], []byte("\n"), 2)[1]
bs[1] = bytes.SplitN(bs[1], []byte("\n"), 2)[1]
if bytes.Compare(bs[0], bs[1]) != 0 {
t.Errorf("Stack didn't match")
t.Errorf("%s", bs[0])
t.Errorf("%s", bs[1])
}
if err.ErrorStack() != err.TypeName()+" "+err.Error()+"\n"+string(err.Stack()) {
t.Errorf("ErrorStack is in the wrong format")
}
}
示例3: statCpu
func statCpu(oC *tCommand) {
var aPass [2][7]int
var aTotl = [2]int{0, 0}
for a := 0; a < len(aPass); a++ {
aBuf, _ := ioutil.ReadFile("/proc/stat")
aLine := bytes.SplitN(aBuf, []byte{'\n'}, 3)
aLine = bytes.SplitN(aLine[1], []byte{' '}, 9)
for aN := 0; aN < len(aPass[0]); aN++ {
aPass[a][aN], _ = strconv.Atoi(string(aLine[aN+1]))
aTotl[a] += aPass[a][aN]
}
if a == 0 {
time.Sleep(time.Duration(150) * time.Millisecond)
}
}
var aArg string
for aN := 0; aN < len(aPass[0]); aN++ {
aPass[0][aN] = (aPass[1][aN] - aPass[0][aN]) * 1000 / (aTotl[1] - aTotl[0])
aArg += fmt.Sprintf("%4d.%d ", aPass[0][aN]/10, aPass[0][aN]%10)
}
oC.buf = []byte(fmt.Sprintf("%-6s %-6s %-6s %-6s %-6s %-6s %-6s\n%s\n",
"User %", "Niced", "System", "Idle", "IOWait", "IRQ", "SoftIRQ", aArg))
if sDebug {
fmt.Println(oC.name)
}
}
示例4: ParseMessage
func ParseMessage(line []byte) *Message {
line = bytes.TrimSpace(line)
if len(line) <= 0 {
return nil
}
m := new(Message)
if line[0] == ':' {
split := bytes.SplitN(line, []byte{' '}, 2)
if len(split) <= 1 {
return nil
}
m.Prefix = string(split[0][1:])
line = split[1]
}
split := bytes.SplitN(line, []byte{':'}, 2)
args := bytes.Split(bytes.TrimSpace(split[0]), []byte{' '})
m.Command = string(bytes.ToUpper(args[0]))
m.Args = make([]string, 0, len(args))
for _, arg := range args[1:] {
m.Args = append(m.Args, string(arg))
}
if len(split) > 1 {
m.Args = append(m.Args, string(split[1]))
}
return m
}
示例5: decodeJob
// Decode job from byte slice
func decodeJob(data []byte) (job *Job, err error) {
if len(data) < 12 {
return nil, common.Errorf("Invalid data: %V", data)
}
datatype := common.BytesToUint32([4]byte{data[4], data[5], data[6], data[7]})
l := common.BytesToUint32([4]byte{data[8], data[9], data[10], data[11]})
if len(data[12:]) != int(l) {
return nil, common.Errorf("Invalid data: %V", data)
}
data = data[12:]
job = &Job{magicCode: common.RES, DataType: datatype, c: make(chan bool)}
switch datatype {
case common.JOB_ASSIGN:
s := bytes.SplitN(data, []byte{'\x00'}, 3)
if len(s) == 3 {
job.Handle = string(s[0])
job.Fn = string(s[1])
data = s[2]
}
case common.JOB_ASSIGN_UNIQ:
s := bytes.SplitN(data, []byte{'\x00'}, 4)
if len(s) == 4 {
job.Handle = string(s[0])
job.Fn = string(s[1])
job.UniqueId = string(s[2])
data = s[3]
}
}
job.Data = data
return
}
示例6: main
func main() {
s := []byte("1,2,3,4")
sep := []byte{','}
print(bytes.SplitN(s, sep, 2))
print(bytes.SplitN(s, sep, 0))
print(bytes.SplitN(s, sep, -1))
}
示例7: decodeInPack
// Decode job from byte slice
func decodeInPack(data []byte) (inpack *inPack, l int, err error) {
if len(data) < minPacketLength { // valid package should not less 12 bytes
err = fmt.Errorf("Invalid data: %V", data)
return
}
dl := int(binary.BigEndian.Uint32(data[8:12]))
dt := data[minPacketLength : dl+minPacketLength]
if len(dt) != int(dl) { // length not equal
err = fmt.Errorf("Invalid data: %V", data)
return
}
inpack = getInPack()
inpack.dataType = binary.BigEndian.Uint32(data[4:8])
switch inpack.dataType {
case dtJobAssign:
s := bytes.SplitN(dt, []byte{'\x00'}, 3)
if len(s) == 3 {
inpack.handle = string(s[0])
inpack.fn = string(s[1])
inpack.data = s[2]
}
case dtJobAssignUniq:
s := bytes.SplitN(dt, []byte{'\x00'}, 4)
if len(s) == 4 {
inpack.handle = string(s[0])
inpack.fn = string(s[1])
inpack.uniqueId = string(s[2])
inpack.data = s[3]
}
default:
inpack.data = dt
}
l = dl + minPacketLength
return
}
示例8: SetBytes
func (o *Tree) SetBytes(b []byte) (err error) {
zr, e := zlib.NewReader(bytes.NewBuffer(b))
if e == nil {
defer zr.Close()
b, err = ioutil.ReadAll(zr)
if err != nil {
return err
}
}
o.Content = b
body := b
for {
split := bytes.SplitN(body, []byte{0}, 2)
split1 := bytes.SplitN(split[0], []byte{' '}, 2)
o.Entries = append(o.Entries, treeEntry{
Mode: string(split1[0]),
File: string(split1[1]),
Hash: split[1][0:20]})
body = split[1][20:]
if len(split[1]) == 20 {
break
}
}
return
}
示例9: CreateSlackMessage
func CreateSlackMessage(eventData []byte) SlackMessage {
// Assumes topic is of the form domain/name/icon␀msg
// but domain is ignored (should probably be slack.scraperwiki.com)
i := bytes.IndexByte(eventData, byte('\x00'))
if i == -1 {
return SlackMessage{
string(eventData),
"dinger",
":broken_heart:",
"#log",
}
}
splitEventData := bytes.SplitN(eventData, []byte("\x00"), 2)
route := splitEventData[0]
text := splitEventData[1]
splitRoute := bytes.SplitN(route, []byte("/"), 3)
name := splitRoute[1]
icon := splitRoute[2]
return SlackMessage{
string(text),
string(name),
string(icon),
"#log",
}
}
示例10: parseRotateEvent
func (blp *Blp) parseRotateEvent(line []byte) {
rem := bytes.SplitN(line, mysqlctl.BINLOG_ROTATE_TO, 2)
rem2 := bytes.SplitN(rem[1], mysqlctl.POS, 2)
rotateFilename := strings.TrimSpace(string(rem2[0]))
rotatePos, err := strconv.ParseUint(string(rem2[1]), 10, 64)
if err != nil {
panic(NewBinlogParseError(fmt.Sprintf("Error in extracting rotate pos %v from line %s", err, string(line))))
}
if !blp.usingRelayLogs {
//If the file being parsed is a binlog,
//then the rotate events only correspond to itself.
blp.currentPosition.Position.MasterFilename = rotateFilename
blp.currentPosition.Position.MasterPosition = rotatePos
blp.globalState.parseStats.Add("BinlogRotate."+blp.keyrangeTag, 1)
} else {
//For relay logs, the rotate events could be that of relay log or the binlog,
//the prefix of rotateFilename is used to test which case is it.
logsDir, relayFile := path.Split(blp.currentPosition.Position.RelayFilename)
currentPrefix := strings.Split(relayFile, ".")[0]
rotatePrefix := strings.Split(rotateFilename, ".")[0]
if currentPrefix == rotatePrefix {
//relay log rotated
blp.currentPosition.Position.RelayFilename = path.Join(logsDir, rotateFilename)
blp.globalState.parseStats.Add("RelayRotate."+blp.keyrangeTag, 1)
} else {
//master file rotated
blp.currentPosition.Position.MasterFilename = rotateFilename
blp.currentPosition.Position.MasterPosition = rotatePos
blp.globalState.parseStats.Add("BinlogRotate."+blp.keyrangeTag, 1)
}
}
}
示例11: TestStackFormatMatches
func TestStackFormatMatches(t *testing.T) {
defer func() {
err := recover()
if err != 'a' {
t.Fatal(err)
}
bs := [][]byte{Errorf("hi").StackTrace(), debug.Stack()}
// Ignore the first line (as it contains the PC of the .Stack() call)
bs[0] = bytes.SplitN(bs[0], []byte("\n"), 2)[1]
bs[1] = bytes.SplitN(bs[1], []byte("\n"), 2)[1]
if bytes.Compare(bs[0], bs[1]) != 0 {
t.Errorf("Stack didn't match")
t.Errorf("%s", bs[0])
t.Errorf("%s", bs[1])
}
}()
err := a()
if err != nil {
t.Error(err)
}
}
示例12: ExampleSplitN
func ExampleSplitN() {
fmt.Printf("%q\n", bytes.SplitN([]byte("a,b,c"), []byte(","), 2))
z := bytes.SplitN([]byte("a,b,c"), []byte(","), 0)
fmt.Printf("%q (nil = %v)\n", z, z == nil)
// Output:
// ["a" "b,c"]
// [] (nil = true)
}
示例13: Parse
/*{{{ func (this *IniConfig) Parse(config_path string) (ConfigContainer, error)
* 解析配置文件
*/
func (this *IniConfig) Parse(config_path string) (ConfigContainer, error) {
fp, err := os.Open(config_path)
defer fp.Close()
if err != nil {
return nil, err
}
cfg := &IniConfigContainer{
//make(map[string]map[string]string),
make(config_node),
}
buff := bufio.NewReader(fp)
var mod string
for {
line, _, err := buff.ReadLine()
if io.EOF == err {
break
}
if bytes.Equal(line, empty) {
continue
}
line = bytes.TrimSpace(line)
if bytes.HasPrefix(line, AnnoPrefix) {
continue
}
if bytes.HasPrefix(line, ModLeft) && bytes.HasSuffix(line, ModRight) {
mod = string(line[1 : len(line)-1])
continue
}
if mod == "" {
continue
}
if _, ok := cfg.data[mod]; ok == false {
cfg.data[mod] = make(config_node)
}
vals := bytes.SplitN(line, Equal, 2)
if len(vals) < 2 {
continue
}
section_val := bytes.SplitN(vals[0], SectionSplit, 2)
if len(section_val) < 2 {
cfg.data[mod].(config_node)[string(bytes.TrimSpace(vals[0]))] = string(bytes.TrimSpace(vals[1]))
} else {
section := string(bytes.TrimSpace(section_val[0]))
if _, ok := cfg.data[mod].(config_node)[section]; ok == false {
cfg.data[mod].(config_node)[section] = make(config_node)
}
cfg.data[mod].(config_node)[section].(config_node)[string(bytes.TrimSpace(section_val[1]))] = string(bytes.TrimSpace(vals[1]))
}
}
return cfg, nil
}
示例14: startAgent
// startAgent executes ssh-agent, and returns a Agent interface to it.
func startAgent(t *testing.T) (client Agent, socket string, cleanup func()) {
if testing.Short() {
// ssh-agent is not always available, and the key
// types supported vary by platform.
t.Skip("skipping test due to -short")
}
bin, err := exec.LookPath("ssh-agent")
if err != nil {
t.Skip("could not find ssh-agent")
}
cmd := exec.Command(bin, "-s")
out, err := cmd.Output()
if err != nil {
t.Fatalf("cmd.Output: %v", err)
}
/* Output looks like:
SSH_AUTH_SOCK=/tmp/ssh-P65gpcqArqvH/agent.15541; export SSH_AUTH_SOCK;
SSH_AGENT_PID=15542; export SSH_AGENT_PID;
echo Agent pid 15542;
*/
fields := bytes.Split(out, []byte(";"))
line := bytes.SplitN(fields[0], []byte("="), 2)
line[0] = bytes.TrimLeft(line[0], "\n")
if string(line[0]) != "SSH_AUTH_SOCK" {
t.Fatalf("could not find key SSH_AUTH_SOCK in %q", fields[0])
}
socket = string(line[1])
line = bytes.SplitN(fields[2], []byte("="), 2)
line[0] = bytes.TrimLeft(line[0], "\n")
if string(line[0]) != "SSH_AGENT_PID" {
t.Fatalf("could not find key SSH_AGENT_PID in %q", fields[2])
}
pidStr := line[1]
pid, err := strconv.Atoi(string(pidStr))
if err != nil {
t.Fatalf("Atoi(%q): %v", pidStr, err)
}
conn, err := net.Dial("unix", string(socket))
if err != nil {
t.Fatalf("net.Dial: %v", err)
}
ac := NewClient(conn)
return ac, socket, func() {
proc, _ := os.FindProcess(pid)
if proc != nil {
proc.Kill()
}
conn.Close()
os.RemoveAll(filepath.Dir(socket))
}
}
示例15: parseMasterPosition
func (blp *Blp) parseMasterPosition(line []byte) {
var err error
rem := bytes.SplitN(line, mysqlctl.BINLOG_END_LOG_POS, 2)
masterPosStr := string(bytes.SplitN(rem[1], mysqlctl.SPACE, 2)[0])
blp.nextStmtPosition, err = strconv.ParseUint(masterPosStr, 10, 64)
if err != nil {
panic(NewBinlogParseError(fmt.Sprintf("Error in extracting master position, %v, sql %v, pos string %v", err, string(line), masterPosStr)))
}
}