-3
var a byte
var b []byte
i know how to do this
c := append(b,a...)

but how do i do this elegantly?

c := append(a,b...) <-- what's the solution does anyone knows?

Would like to have the c[0] == a, c[1:] == b for checking next time

Alpha1
  • 1
  • 1
  • 2

2 Answers2

2

You can make a a slice as well then append it with b.

c := append([]byte{a}, b...)
gzcz
  • 496
  • 3
  • 7
1

You can use the bytes.Buffer to make this cleaner:

var a byte
var buf bytes.Buffer

buf.WriteByte(a)// For a single byte
buf.Write([]byte{a})// For byte slice
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Ashwin Shirva
  • 1,331
  • 1
  • 12
  • 31
  • Why bytes.buffer? is it slower or faster? – Alpha1 Jun 15 '20 at 21:14
  • It is a handy wrapper around byte slice, and also it implements several interfaces, io.Reader, io.Writer to name a few. It is an ideal choice when you have a lot of values to append, and also it provides methods for efficient conversion of byte slice to string. – Ashwin Shirva Jun 16 '20 at 02:17