How to assign string to bytes array

go
sofire picture sofire · Nov 7, 2011 · Viewed 413.5k times · Source

I want to assign string to bytes array:

var arr [20]byte
str := "abc"
for k, v := range []byte(str) {
  arr[k] = byte(v)
}

Have another method?

Answer

openwonk picture openwonk · Feb 1, 2015

Safe and simple:

[]byte("Here is a string....")