newbie: string to byte array

Showing 1-4 of 5 messages
newbie: string to byte array Thomas Kaufmann 12/9/10 4:57 AM
Hi,

how can I convert a string in a byte array?

o-o

Thomas
Re: [go-nuts] newbie: string to byte array Rob 'Commander' Pike 12/9/10 4:59 AM
On Thu, Dec 9, 2010 at 7:57 AM, Thomas Kaufmann <tok...@googlemail.com> wrote:
> Hi,
>
> how can I convert a string in a byte array?

By converting a string [to] a byte array:

    []byte("string")

-rob

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:
   T(v)

So, to do the reverse of Rob's example, you can convert the []byte b
to a string like so:
   string(b)

Andrew

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)