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
4
  • 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. Commented Nov 20, 2017 at 7: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. Commented Nov 20, 2017 at 7: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 tour.golang.org/moretypes/15 ad the referenced blog.golang.org/go-slices-usage-and-internals. Commented Nov 20, 2017 at 8:00
  • 3
    There are no arrays in your example, only slices. Please read this. Commented Nov 20, 2017 at 8:01

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, so my attempt to append a byte array to a byte slice is not logical then ? I thought append would just attach the two together.
@Sir: See my revised answer.
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

2 Comments

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 :)
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.