简体   繁体   中英

How to deal with byte operators in Golang?

I would like to create a buffer which will contain information like nickname and password. Let's say I have created empty buffer, which is

00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

By then I would like to fill it up with data, like buffer << nickname(string) << password(string) , so in result I'm getting

08 75 73 65 72 6e 61 6d 65 08 70 61 73 73 77 6f 72 64

which is len.nickname actual_nickname len.password password

Now, after I have created such buffer I would like to parse it to variables. That would look like buffer >> nickname(string) >> password(string)

I made something once in c++ which was using operators, but I am not sure how to do so in Golang.

These buffers would be used as packets body in my networking app. I don't want to use structs, but kind of above.

I hope I explained my issue properly. I also do not want to use splitters like : to get these into array table.

Thank you.

Try using a bytes.Buffer , to which you can write as necessary, including using fmt.Fprintf if necessary. When everything is written to the buffer, you can get the bytes back out.

buf := bytes.NewBuffer(nil)
fmt.Fprint(buf, nickname)
fmt.Fprint(buf, password)
fmt.Print(buf.Bytes())

Working example here: https://play.golang.org/p/bRL6-N-3qH_n

You can do fixed-size encoding using the encoding/binary package, including choosing endianness.

The example below writes two strings preceded by the length stored as a uint32 in network byte order. This would be appropriate to send over the wire. You can use uint16 , or even uint8 if you're certain this will be enough to represent the string lengths.

Warning : I'm ignoring all errors in this sample code. Please do not do that in yours.

package main

import (
    "bytes"
    "encoding/binary"
    "fmt"
)

func main() {
    user := "foouser"
    password := "barpassword"

    // Write "len(user) user len(password) password".
    // Lengths are unsigned 32 bit ints in network byte order.
    var buf bytes.Buffer
    binary.Write(&buf, binary.BigEndian, (uint32)(len(user)))
    buf.WriteString(user)
    binary.Write(&buf, binary.BigEndian, (uint32)(len(password)))
    buf.WriteString(password)

    var output []byte = buf.Bytes()
    fmt.Printf("% x\n", output)

    // Now read it back (we could use the buffer,
    // but let's use Reader for clarity)
    input := bytes.NewReader(output)
    var uLen, pLen uint32

    binary.Read(input, binary.BigEndian, &uLen)
    uBytes := make([]byte, uLen)
    input.Read(uBytes)
    newUser := string(uBytes)

    binary.Read(input, binary.BigEndian, &pLen)
    pBytes := make([]byte, pLen)
    input.Read(pBytes)
    newPassword := string(pBytes)

    fmt.Printf("User: %q, Password: %q", newUser, newPassword)
}

This outputs the byte array and the strings extracted from it:

00 00 00 07 66 6f 6f 75 73 65 72 00 00 00 0b 62 61 72 70 61 73 73 77 6f 72 64
User: "foouser", Password: "barpassword"

playground link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM