Convert []string to []byte

后端 未结 6 1602
梦谈多话
梦谈多话 2021-02-07 01:36

I am looking to convert a string array to a byte array in GO so I can write it down to a disk. What is an optimal solution to encode and decode a string array ([]string

6条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-07 02:29

    To illustrate the problem, convert []string to []byte and then convert []byte back to []string, here's a simple solution:

    package main
    
    import (
        "encoding/binary"
        "fmt"
    )
    
    const maxInt32 = 1<<(32-1) - 1
    
    func writeLen(b []byte, l int) []byte {
        if 0 > l || l > maxInt32 {
            panic("writeLen: invalid length")
        }
        var lb [4]byte
        binary.BigEndian.PutUint32(lb[:], uint32(l))
        return append(b, lb[:]...)
    }
    
    func readLen(b []byte) ([]byte, int) {
        if len(b) < 4 {
            panic("readLen: invalid length")
        }
        l := binary.BigEndian.Uint32(b)
        if l > maxInt32 {
            panic("readLen: invalid length")
        }
        return b[4:], int(l)
    }
    
    func Decode(b []byte) []string {
        b, ls := readLen(b)
        s := make([]string, ls)
        for i := range s {
            b, ls = readLen(b)
            s[i] = string(b[:ls])
            b = b[ls:]
        }
        return s
    }
    
    func Encode(s []string) []byte {
        var b []byte
        b = writeLen(b, len(s))
        for _, ss := range s {
            b = writeLen(b, len(ss))
            b = append(b, ss...)
        }
        return b
    }
    
    func codecEqual(s []string) bool {
        return fmt.Sprint(s) == fmt.Sprint(Decode(Encode(s)))
    }
    
    func main() {
        var s []string
        fmt.Println("equal", codecEqual(s))
        s = []string{"", "a", "bc"}
        e := Encode(s)
        d := Decode(e)
        fmt.Println("s", len(s), s)
        fmt.Println("e", len(e), e)
        fmt.Println("d", len(d), d)
        fmt.Println("equal", codecEqual(s))
    }
    

    Output:

    equal true
    s 3 [ a bc]
    e 19 [0 0 0 3 0 0 0 0 0 0 0 1 97 0 0 0 2 98 99]
    d 3 [ a bc]
    equal true
    

提交回复
热议问题