本文整理汇总了Golang中bytes.Add函数的典型用法代码### 示例。如果您正苦于以下问题:Golang Add函数的具体用法?Golang Add怎么用?Golang Add使用的例子?那么恭喜您, 这里精选的函数代码### 示例或许可以为您提供帮助。

在下文中一共展示了Add函数的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。

示例1: readUntilCrLf

func readUntilCrLf(con *net.TCPConn) (line []byte, err os.Error) {

    buf := make([]byte, 1)
    var data []byte
    crSeen := false

    for {
        _, err := con.Read(buf)
        if err != nil {
            if err == os.EOF {
                break
            } else {
                return nil, err
            }
        }
        if crSeen {
            if buf[0] == 10 {
                break
            } else {
                crSeen = false
                data = bytes.Add(data, buf)
            }
        } else {
            if buf[0] == 13 {
                crSeen = true
            } else {
                data = bytes.Add(data, buf)
            }
        }
    }

    return data, nil
}

开发者ID:jmettraux,项目名称:glaive,代码行数:33,代码来源:main.go

示例2: setPrefix

// Extract regular text from the beginning of the pattern.
// That text can be used by doExecute to speed up matching.
func (re *Regexp) setPrefix() {
    var b []byte
    var utf = make([]byte, utf8.UTFMax)
    // First instruction is start; skip that.
    i := re.inst.At(0).(instr).next().index()
Loop:
    for i < re.inst.Len() {
        inst := re.inst.At(i).(instr)
        // stop if this is not a char
        if inst.kind() != _CHAR {
            break
        }
        // stop if this char can be followed by a match for an empty string,
        // which includes closures, ^, and $.
        switch re.inst.At(inst.next().index()).(instr).kind() {
        case _BOT, _EOT, _ALT:
            break Loop
        }
        n := utf8.EncodeRune(inst.(*_Char).char, utf)
        b = bytes.Add(b, utf[0:n])
        i = inst.next().index()
    }
    // point prefixStart instruction to first non-CHAR after prefix
    re.prefixStart = re.inst.At(i).(instr)
    re.prefixBytes = b
    re.prefix = string(b)
}

开发者ID:ivanwyc,项目名称:google-go,代码行数:29,代码来源:regexp.go

示例3: UnmarshalJSON

// UnmarshalJSON sets *m to a copy of data.
func (m *RawMessage) UnmarshalJSON(data []byte) os.Error {
    if m == nil {
        return os.NewError("json.RawMessage: UnmarshalJSON on nil pointer")
    }
    *m = bytes.Add((*m)[0:0], data)
    return nil
}

开发者ID:rapgamer,项目名称:golang-china,代码行数:8,代码来源:stream.go

示例4: readHandshake

// readHandshake reads the next handshake message from
// the record layer.
// c.in.Mutex < L; c.out.Mutex < L.
func (c *Conn) readHandshake() (interface{}, os.Error) {
    for c.hand.Len() < 4 {
        if c.err != nil {
            return nil, c.err
        }
        c.readRecord(recordTypeHandshake)
    }

    data := c.hand.Bytes()
    n := int(data[1])<<16 | int(data[2])<<8 | int(data[3])
    if n > maxHandshake {
        c.sendAlert(alertInternalError)
        return nil, c.err
    }
    for c.hand.Len() < 4+n {
        if c.err != nil {
            return nil, c.err
        }
        c.readRecord(recordTypeHandshake)
    }
    data = c.hand.Next(4 + n)
    var m handshakeMessage
    switch data[0] {
    case typeClientHello:
        m = new(clientHelloMsg)
    case typeServerHello:
        m = new(serverHelloMsg)
    case typeCertificate:
        m = new(certificateMsg)
    case typeCertificateRequest:
        m = new(certificateRequestMsg)
    case typeCertificateStatus:
        m = new(certificateStatusMsg)
    case typeServerHelloDone:
        m = new(serverHelloDoneMsg)
    case typeClientKeyExchange:
        m = new(clientKeyExchangeMsg)
    case typeCertificateVerify:
        m = new(certificateVerifyMsg)
    case typeNextProtocol:
        m = new(nextProtoMsg)
    case typeFinished:
        m = new(finishedMsg)
    default:
        c.sendAlert(alertUnexpectedMessage)
        return nil, alertUnexpectedMessage
    }

    // The handshake message unmarshallers
    // expect to be able to keep references to data,
    // so pass in a fresh copy that won't be overwritten.
    data = bytes.Add(nil, data)

    if !m.unmarshal(data) {
        c.sendAlert(alertUnexpectedMessage)
        return nil, alertUnexpectedMessage
    }
    return m, nil
}

开发者ID:GNA-SERVICES-INC,项目名称:MoNGate,代码行数:62,代码来源:conn.go

示例5: EncodeSOA

func EncodeSOA(soa SOArecord) []byte {
    var (
        result []byte
        temp32 []byte
    )
    mname := Encode(soa.Mname)
    length := len(mname)
    rname := Encode(soa.Rname)
    length = length + len(rname)
    length = length + (5 * 4) // Five 32-bits counter at the end
    /* "It's probably cleaner to write to a bytes.Buffer than to
    repeatedly call bytes.Add." Russ Cox, go-nuts ML */
    result = bytes.Add(result, mname)
    result = bytes.Add(result, rname)
    temp32 = make([]byte, 4)
    binary.BigEndian.PutUint32(temp32, soa.Serial)
    result = bytes.Add(result, temp32)
    binary.BigEndian.PutUint32(temp32, soa.Refresh)
    result = bytes.Add(result, temp32)
    binary.BigEndian.PutUint32(temp32, soa.Retry)
    result = bytes.Add(result, temp32)
    binary.BigEndian.PutUint32(temp32, soa.Expire)
    result = bytes.Add(result, temp32)
    binary.BigEndian.PutUint32(temp32, soa.Minimum)
    result = bytes.Add(result, temp32)
    return result[0:length]
}

开发者ID:bortzmeyer,项目名称:grong,代码行数:27,代码来源:types.go

示例6: sendMessageToReply

// To use with messages that receive a response from database
// 'opQuery', 'opGetMore'.
func (self *Connection) sendMessageToReply(m message, reqID int32) os.Error {
    body := m.Bytes()
    h := header(msgHeader{int32(len(body) + _HEADER_SIZE), reqID, 0, m.OpCode()})

    msg := bytes.Add(h, body)
    _, err := self.conn.Write(msg)

    return err
}

开发者ID:anvie,项目名称:gomongo,代码行数:11,代码来源:collection.go

示例7: Decode

func Decode(b []byte) []byte {
    r := make([]byte, len(b)*2)[0:0]
    for {
        if b[0] != 128 {
            break
        }
        if b[1] == 3 {
            break
        }
        l := int(b[2]) + (int(b[3]) << 8) + (int(b[4]) << 16) + 6
        if b[1] == 1 {
            r = bytes.Add(r, b[6:l])
        } else {
            r = bytes.Add(r, hex.Encode(b[6:l]))
        }
        b = b[l:]
    }
    return r
}

开发者ID:MattDavisRV,项目名称:pdfreader,代码行数:19,代码来源:pfb.go

示例8: writeMessage

func (c *Connection) writeMessage(m message) os.Error {
    body := m.Bytes()
    hb := header(int32(len(body)+16), m.RequestID(), 0, m.OpCode())
    msg := bytes.Add(hb, body)

    _, err := c.conn.Write(msg)

    last_req = m.RequestID()
    return err
}

开发者ID:patrickxb,项目名称:gomongo,代码行数:10,代码来源:mongo.go

示例9: Bytes

func (self *_Object) Bytes() []byte {
    buf := bytes.NewBuffer([]byte{})
    for k, v := range self.value {
        buf.WriteByte(byte(v.Kind()))
        buf.WriteString(k)
        buf.WriteByte(0)
        buf.Write(v.Bytes())
    }
    buf.WriteByte(0)

    l := buf.Len() + 4
    w32 := make([]byte, _WORD32)
    pack.PutUint32(w32, uint32(l))
    return bytes.Add(w32, buf.Bytes())
}

开发者ID:nstott,项目名称:gomongo,代码行数:15,代码来源:bson.go

示例10: Repeat

func Repeat(alu []byte, n int) {
    buf := bytes.Add(alu, alu)
    off := 0
    for n > 0 {
        m := n
        if m > Line {
            m = Line
        }
        buf1 := out.NextWrite(m + 1)
        copy(buf1, buf[off:])
        buf1[m] = '\n'
        if off += m; off >= len(alu) {
            off -= len(alu)
        }
        n -= m
    }
}

开发者ID:IntegerCompany,项目名称:linaro-android-gcc,代码行数:17,代码来源:fasta.go

示例11: simpleQuery

func (self *Client) simpleQuery(command int, version int, size int, body []byte) ([]byte, os.Error) {
    conn, err := self.connect()
    if err != nil {
        return nil, err
    }

    header := make([]byte, 8+len(body))
    binary.BigEndian.PutUint16(header[0:2], uint16(command))
    binary.BigEndian.PutUint16(header[2:4], uint16(version))
    binary.BigEndian.PutUint32(header[4:8], uint32(size))

    request := bytes.Add(header, body)

    _, err = conn.Write(request)
    if err != nil {
        return nil, err
    }

    return self.getResponse(conn)
}

开发者ID:kpumuk,项目名称:gosphinx,代码行数:20,代码来源:sphinx.go

示例12: ReadAll

// ReadAll takes an os.File as it's argument and reads in all of it until 
// it signals to stop with an EOF in 
func ReadAll(file * os.File) ([]byte, os.Error) { 
  const BUFSIZE = 1024
  result    := make([]byte, 0)
  buf       := make([]byte, BUFSIZE)
  var sub []byte 
  for { // ever     
    count, err2 := file.Read(buf)
    sub          = buf[0:count]
    // get right slice to append to result
    // append bytes to result
    if count > 0 { 
      result = bytes.Add(result, sub)
    }
    // if EOF, return the result. 
    if err2 == os.EOF { return result, nil }
    // on any other error return nil 
    if err2 != nil    { return nil   , err2 }    
  }
  // should not get here  
  return nil, os.NewError("Can't happen?!");
}

开发者ID:beoran,项目名称:fungo,代码行数:23,代码来源:gut.go

示例13: TestRecords

func TestRecords(t *testing.T) {
    for i, test := range recordsTests {
        buf := bytes.NewBuffer(test.raw)
        content, reqId, kind, err := readRecord(buf)
        if err != nil {
            t.Errorf("%d error reading record: %s", i, err.String())
            goto write
        }
        for buf.Len() > 0 {
            moreContent, _, _, err := readRecord(buf)
            if err != nil {
                t.Errorf("%d error reading additional record: %s", i, err.String())
                goto write
            }
            content = bytes.Add(content, moreContent)
        }
        if kind != test.kind {
            t.Errorf("%d got kind %d expected %d", i, kind, test.kind)
        }
        if reqId != test.reqId {
            t.Errorf("%d got request ID %d expected %d", i, kind, test.kind)
        }
        if !bytes.Equal(content, test.content) {
            t.Errorf("%d read wrong content", i)
        }
    write:
        buf.Reset()
        c := &lockReadWriteCloser{ReadWriteCloser: &nilCloser{buf}}
        err = writeRecord(c, test.kind, test.reqId, test.content)
        if err != nil {
            t.Errorf("%d error writing record: %s", i, err.String())
            continue
        }
        if !bytes.Equal(buf.Bytes(), test.raw) {
            t.Errorf("%d wrote wrong content", i)
        }
    }
}

开发者ID:gnanderson,项目名称:fcgigo,代码行数:38,代码来源:fcgi_test.go

示例14: TestNextValueBig

func TestNextValueBig(t *testing.T) {
    var scan scanner
    item, rest, err := nextValue(jsonBig, &scan)
    if err != nil {
        t.Fatalf("nextValue: ", err)
    }
    if len(item) != len(jsonBig) || &item[0] != &jsonBig[0] {
        t.Errorf("invalid item: %d %d", len(item), len(jsonBig))
    }
    if len(rest) != 0 {
        t.Errorf("invalid rest: %d", len(rest))
    }

    item, rest, err = nextValue(bytes.Add(jsonBig, []byte("HELLO WORLD")), &scan)
    if err != nil {
        t.Fatalf("nextValue extra: ", err)
    }
    if len(item) != len(jsonBig) {
        t.Errorf("invalid item: %d %d", len(item), len(jsonBig))
    }
    if string(rest) != "HELLO WORLD" {
        t.Errorf("invalid rest: %d", len(rest))
    }
}

开发者ID:rapgamer,项目名称:golang-china,代码行数:24,代码来源:scanner_test.go

示例15: ReadContinuedLineBytes

// ReadContinuedLineBytes is like ReadContinuedLine but
// returns a []byte instead of a string.
func (r *Reader) ReadContinuedLineBytes() ([]byte, os.Error) {
    // Read the first line.
    line, err := r.ReadLineBytes()
    if err != nil {
        return line, err
    }
    if len(line) == 0 { // blank line - no continuation
        return line, nil
    }
    line = trim(line)

    // Look for a continuation line.
    c, err := r.R.ReadByte()
    if err != nil {
        // Delay err until we read the byte next time.
        return line, nil
    }
    if c != ' ' && c != '\t' {
        // Not a continuation.
        r.R.UnreadByte()
        return line, nil
    }

    // Read continuation lines.
    for {
        // Consume leading spaces; one already gone.
        for {
            c, err = r.R.ReadByte()
            if err != nil {
                break
            }
            if c != ' ' && c != '\t' {
                r.R.UnreadByte()
                break
            }
        }
        var cont []byte
        cont, err = r.ReadLineBytes()
        cont = trim(cont)
        line = bytes.Add(line, space)
        line = bytes.Add(line, cont)
        if err != nil {
            break
        }

        // Check for leading space on next line.
        if c, err = r.R.ReadByte(); err != nil {
            break
        }
        if c != ' ' && c != '\t' {
            r.R.UnreadByte()
            break
        }
    }

    // Delay error until next call.
    if len(line) > 0 {
        err = nil
    }
    return line, err
}

开发者ID:GNA-SERVICES-INC,项目名称:MoNGate,代码行数:63,代码来源:reader.go

示例16: TestEncodeFloat64

func TestEncodeFloat64(t *testing.T) {
    expected := bytes.Add([]byte{MAGIC, FLOAT}, strings.Bytes(fmt.Sprintf("%.20e", float64(1.2e3))))
    testEncode(t, "Encoding float64 failed", 1.2e3, expected)
}

开发者ID:nicholassm,项目名称:gobert,代码行数:4,代码来源:bert_test.go

示例17: TestEncodeString

func TestEncodeString(t *testing.T) {
    expected := bytes.Add([]byte{MAGIC, BIN, 0, 0, 0, 3}, strings.Bytes("foo"))
    testEncode(t, "Encoding string failed", "foo", expected)
}

开发者ID:nicholassm,项目名称:gobert,代码行数:4,代码来源:bert_test.go

示例18: unmarshal


//.........这里部分代码省略.........
            // If that fails, fall back to mop-up field named "Any".
            if sv != nil {
                k := fieldName(t.Name.Local)
                any := -1
                for i, n := 0, styp.NumField(); i < n; i++ {
                    f := styp.Field(i)
                    if strings.ToLower(f.Name) == k {
                        if err := p.unmarshal(sv.FieldByIndex(f.Index), &t); err != nil {
                            return err
                        }
                        continue Loop
                    }
                    if any < 0 && f.Name == "Any" {
                        any = i
                    }
                }
                if any >= 0 {
                    if err := p.unmarshal(sv.FieldByIndex(styp.Field(any).Index), &t); err != nil {
                        return err
                    }
                    continue Loop
                }
            }
            // Not saving sub-element but still have to skip over it.
            if err := p.Skip(); err != nil {
                return err
            }

        case EndElement:
            break Loop

        case CharData:
            if saveData != nil {
                data = bytes.Add(data, t)
            }

        case Comment:
            if saveComment != nil {
                comment = bytes.Add(comment, t)
            }
        }
    }

    var err os.Error
    // Helper functions for integer and unsigned integer conversions
    var itmp int64
    getInt64 := func() bool {
        itmp, err = strconv.Atoi64(string(data))
        // TODO: should check sizes
        return err == nil
    }
    var utmp uint64
    getUint64 := func() bool {
        utmp, err = strconv.Atoui64(string(data))
        // TODO: check for overflow?
        return err == nil
    }
    var ftmp float64
    getFloat64 := func() bool {
        ftmp, err = strconv.Atof64(string(data))
        // TODO: check for overflow?
        return err == nil
    }

    // Save accumulated data and comments
    switch t := saveData.(type) {

开发者ID:lougxing,项目名称:golang-china,代码行数:67,代码来源:read.go

示例19: main

func main() {
    pg_adr, err := net.ResolveTCPAddr("152.7.16.113:5432")
    if err != nil {
        log.Exitf("Error while resolving address: %s", err)
    }
    conn, err := net.DialTCP("tcp", nil, pg_adr)
    if err != nil {
        log.Exitf("Error while connecting: %s", err)
    }
    log.Stdout("I think we connected")

    strs := make([]string, 4)
    strs[0] = "user"
    strs[1] = "alex"
    strs[2] = "database"
    strs[3] = "ar_test"
    str2 := strings.Join(strs, "\x00") + "\x00\x00"

    mesg2 := strings.Bytes(str2)
    hmesg := make([]byte, 4+4)
    binary.BigEndian.PutUint32(hmesg[4:8], uint32(3<<16))
    hmesg = bytes.Add(hmesg, mesg2)
    binary.BigEndian.PutUint32(hmesg[0:4], uint32(len(hmesg)))
    n, err := conn.Write(hmesg)

    if err != nil {
        log.Exitf("Error writing TCP: %s", err)
    }
    log.Stdoutf("wrote %d", n)

    result := make([]byte, 12)
    // the largest response we can get is 12 bytes
    if n, err = conn.Read(result); err != nil {
        log.Exitf("Error reading TCP (Read %d bytes): %s", n, err)
    }
    log.Stdoutf("%c", result[0])

    switch string(result[0]) {
    case psql_constants.Authentication:
        log.Stdout("Got authentication message")
    default:
        log.Exit("Did not get authentication message")
    }

    mesglen := binary.BigEndian.Uint32(result[1:5])
    mesgtype := binary.BigEndian.Uint32(result[5:9])

    fmt.Println(mesglen, mesgtype)

    passhash := md5.New()
    passhash.Write(strings.Bytes("jar1!" + "alex"))
    salthash := md5.New()
    salthash.Write(passhash.Sum())
    salthash.Write(result[9:12])

    passresponse := make([]byte, 5)
    passresponse[0] = 'p'
    binary.BigEndian.PutUint32(passresponse[1:5], uint32(4+salthash.Size()+1))
    passresponse = bytes.Add(passresponse, strings.Bytes("md5"))
    passresponse = bytes.Add(passresponse, salthash.Sum())
    passresponse = bytes.AddByte(passresponse, byte(0))
    fmt.Println(passresponse)
    n, err = conn.Write(passresponse)
    if err != nil {
        log.Exitf("Error writing TCP: %s", err)
    }
    log.Stdoutf("wrote %d", n)

    result = make([]byte, 18)
    // the largest response we can get is 12 bytes
    if n, err = conn.Read(result); err != nil {
        log.Exitf("Error reading TCP (Read %d bytes): %s", n, err)
    }
    fmt.Println(result)

    conn.Close()
}

开发者ID:machinaut,项目名称:go-play,代码行数:77,代码来源:psql.go

示例20: run

func (c *Client) run(complete chan bool) {
    print("client running\n")
    print("sending handshake\n")
    c.HandShake()
    print("waiting for handshake\n")
    if !c.WaitHandShake() {
        complete <- false
        return
    }

    c.processHandShake(msg)
    blank := make([]byte, 9)
    msg := make([]byte, 49+pstrlen)
    buffer := bytes.NewBuffer(msg)
    buffer.WriteByte(pstrlen)
    buffer.WriteString(c.torrent.Pstr)
    buffer.Write(blank)
    buffer.Write(&(c.torrent.info_hash))
    buffer.Write(&(c.torrent.peer_id))
    _, err = c.conn.Write(buffer.Bytes())
    return

    for {
        print("waiting for msg\n")
        _, err := c.conn.Read(c.buffer[0:4]) //read msg length
        if err != nil {
            complete <- false
            return
        }

        length := binary.BigEndian.Uint32(c.buffer[0:4])
        print("recieved msg length" + strconv.Itoa(int(length)) + "\n")
        switch {
        case length == 0:
            print("keepalive msg")
            continue

        case length >= MaxInMsgSize:
            complete <- true
            return
        }
        _, err = c.conn.Read(c.buffer[4 : 4+length])
        if err != nil {
            complete <- true
            return
        }
        print("msg recieved payload\n")
        msg := new(message)
        msg.length = length
        msg.msgId = c.buffer[4]
        bytes.Add(msg.payLoad, c.buffer[5:len(c.buffer)])
        err = c.processMsg(msg)
        if err != nil {
            print(err.String())
            complete <- true
            return
        }
        runtime.Gosched()

    }

}

开发者ID:peernode,项目名称:gobit,代码行数:62,代码来源:gotorrent.go

最后编辑: goerone  文档更新时间: 2021-08-23 19:14   作者:goerone