2

I have a simple bit of code that i am trying to understand, but am struggling to work out how to get it to work properly.

The general idea is i want to pass some data, and convert it into a byte array. Then i want to apply the length of the byte array at the first index of my byte slice, then add the byte array to end of the slice.

This is what it tried:

    var slice []byte
    myString := "Hello there"

    stringAsByteArray := []byte(myString) //convert my string to byte array


    slice[0] = byte(len(stringAsByteArray)) //length of string as byte array

    append(slice, stringAsByteArray) 

So the idea is the first byte of slice contains the number of len(b) then following on from that, the actual string message as a series of bytes.

But i get:

cannot use stringAsByteArray (type []byte) as type byte in append
append(slice, stringAsByteArray) evaluated but not used
JimB
  • 104,193
  • 13
  • 262
  • 255
Sir
  • 8,135
  • 17
  • 83
  • 146
  • What exactly is unclear with the error messages printed by the compiler: strings and []bytes are different types and you cannot mix types in append. The second is even more fundamental. All this is explained much better in the Tour of Go than could be explained here. – Volker Nov 20 '17 at 07:22
  • I thought it was appending a byte array since i did convert it to a byte array..and seeing as my slice is bytes not a string. I don't fully understand why it couldn't append a byte array to a slice of bytes. – Sir Nov 20 '17 at 07:23
  • " I don't fully understand why it couldn't append a byte array to a slice of bytes". Simply because `append` works typically like this `append([]T, T)`: You do not append a slice to a slice you append one or more elements to a slice. If you want to append each element of a slice use `...` which is explained very well in https://tour.golang.org/moretypes/15 ad the referenced https://blog.golang.org/go-slices-usage-and-internals. – Volker Nov 20 '17 at 08:00
  • 3
    There are no arrays in your example, only slices. Please read [this](https://blog.golang.org/slices). – kostix Nov 20 '17 at 08:01

2 Answers2

2

For example,

package main

import "fmt"

func main() {
    myString := "Hello there"
    slice := make([]byte, 1, 1+len(myString))
    slice[0] = byte(len(myString))
    slice = append(slice, myString...)
    fmt.Println(slice[0], string(slice[1:]))
}

Output:

11 Hello there
peterSO
  • 158,998
  • 31
  • 281
  • 276
0
  1. First of all you need to initialize slice with make function
  2. To add data to slice you should use append function
  3. To add elements from []byte to []byte you can use spread operator in append call.
  4. And finally you need to use result of calling append function

https://play.golang.org/p/CP18akKsCa

RidgeA
  • 1,506
  • 1
  • 10
  • 18
  • Doesn't this add the length of string to the end of the slice not the start? I was trying to get it at the start :) – Sir Nov 20 '17 at 07:29
  • Are you about `myString := "Hello there"; slice := make([]byte, 0, len(myString)+1)` ? This create slice with *length 0* and *capacity len(string) + 1*. This is small optimization to allocate all memory for slice that you will need to create byte array from string + first byte with string length. – RidgeA Nov 20 '17 at 07:37