How to append byte array to slice of bytes in Go

10,286

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
Share:
10,286

Related videos on Youtube

Sir
Author by

Sir

Hello :)

Updated on June 04, 2022

Comments

  • Sir
    Sir almost 2 years

    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
    
    • Volker
      Volker over 6 years
      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.
    • Sir
      Sir over 6 years
      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.
    • Volker
      Volker over 6 years
      " 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.
    • kostix
      kostix over 6 years
      There are no arrays in your example, only slices. Please read this.
  • Sir
    Sir over 6 years
    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
    Sir over 6 years
    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 :)
  • peterSO
    peterSO over 6 years
    @Sir: See my revised answer.
  • RidgeA
    RidgeA over 6 years
    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.