|newbie: string to byte array||Thomas Kaufmann||12/9/10 4:57 AM|
how can I convert a string in a byte array?
|Re: [go-nuts] newbie: string to byte array||Rob 'Commander' Pike||12/9/10 4:59 AM|
|Re: [go-nuts] newbie: string to byte array||Andrew Gerrand||12/9/10 1:57 PM|
To expand a little: this is how type conversions work more generally in Go.
To convert variable v to type T, you would do:
So, to do the reverse of Rob's example, you can convert the byte b
|Re: newbie: string to byte array||Jim Teeuwen||12/9/10 3:20 PM|
Incidentally, this also works for fetching unicode runes from a string
and in reverse:
runes := int("foo")
str := string(runes)
|Re: [go-nuts] newbie: string to byte array||Mark Tomczak||6/7/15 9:30 AM|
Quick point of clarificaton (for my own understanding): This converts the string to a byte slice, right? Converting to a byte array would imply that the length of the array were statically fixed at compile time, and there's no way to statically guarantee the string has a particular length.
(This is still almost certainly the right thing to do; I just want to confirm I'm not misunderstanding the typing rules in this context).
|Re: newbie: string to byte array||Ashish Srivastava||1/19/16 5:34 AM|
|Re: [go-nuts] newbie: string to byte array||Roberto Zanotto||1/19/16 7:47 AM|
Yes, this converts the string to a byte slice. If you need a byte array and you know the length of the string at compile time (or an upper bound), you can use copy: