Scenario: In development, to combine multiple []byte arrays into one []byte], the initial implementation is as follows:
1. Get multiple []byte lengths
2. Construct a two-dimensional code array
3. loop copies []byte to a two-dimensional array
Package Gstoreimport ( "bytes")//bytescombine Multiple []byte arrays merged into one []bytefunc Bytescombine (Pbytes ... []byte) []byte { len:= len (pbytes) s:= make ([]byte, Len) for index: = 0; Index < Len; index++ { = Pbytes[index] } Sep:= []byte("") return bytes. Join (S, Sep)}
Combined with the bytes feature, you can use the Join function to merge, as follows:
Package Gstoreimport ( "bytes")//bytescombine Multiple []byte arrays merged into one []bytefunc Bytescombine (Pbytes ... []byte) []byte { return bytes. Join (Pbytes, []byte(""))}
It's so cool. ~~~~~~~~~~ to understand language features and refactor. This is the element of success.
Go language: Multiple []byte arrays merged into one []byte