V
V
Vladimir Grabko2016-08-07 10:35:19
go
Vladimir Grabko, 2016-08-07 10:35:19

Is it possible to do this better?

My task is as follows:
I have 100500 elements. key, value []byte
I need to collect all this in 1 []byte and easily decode it into 100500 key, value []byte.
Now I have reached the baseline. Bytes circulate over the network in this format

type D struct {
  C          uint8
  Db         string
  Key, Value []byte
}

with length indicators in the first bytes.
It is necessary to push into Value:
type Map struct {
  Key, Value []byte
}

I got to an algorithm of this kind
9|12|key|value where the first two elements say the length of the key and value. Then I pull the length from the slice and move on to the next
UPD pointer.
I didn’t write it very clearly, so I’ll attach the finished test code
func EncodeDecodeTest(t *testing.T) {

  d := []uint8{Get, Set, Up, Del}
  for key := range d {
    g := D{}
    g.C = d[key]
    g.Db = "db"
    g.Key = []byte("sdfsdfsdfsdfsdf")
    g.Value = []byte("dsdfsdfs35345dfsdf")
    gg := Decode(Encode(g))

    if g.C != gg.C {
      t.Error(g.C, gg.C)
    }
    if g.Db != gg.Db {
      t.Error(g.Db, gg.Db)
    }
    if !slice.ByteSame(g.Key, gg.Key) {
      t.Error(g.Key, gg.Key)
    }
    if !slice.ByteSame(g.Value, gg.Value) {
      t.Error(g.Value, gg.Value)
    }

  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita, 2016-08-07
@VGrabko

What the hell are you doing? The task is minute.
https://www.google.com/?ion=1&espv=2#q=golang+serialize
https://www.google.com/?ion=1&espv=2#q=golang+json
https://www. google.com/?ion=1&espv=2#q=golang+xml
https://www.google.com/?ion=1&espv=2#q=golang+bina...
https://www.google.com /?ion=1&espv=2#q=golang+gob

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question