initializing a byte array

14,490 views
Skip to first unread message

rcharlet

unread,
Jul 15, 2010, 3:21:06 PM7/15/10
to golang-nuts
Howdy,
I'm in week 3 of going. I was looking for a way to initialize a
[]byte slice with some data so I could subsequently send it in my
net.Conn:Write test I'm building up. After digging around I found this
in one of the tutorials:

buf := []byte("howdy\n")


That is successful for me.

But my question to the list is, how should I have known that? I
can't see anything in the language spec under slices or strings that
suggest one can post-pend a parenthesized string on a []byte
declaration. Please either educate me how to read the langage spec or
update the doc.

Hopefully,

Ricky Charlet

Corey Thomasson

unread,
Jul 15, 2010, 3:26:45 PM7/15/10
to rcharlet, golang-nuts

Evan Shaw

unread,
Jul 15, 2010, 3:29:19 PM7/15/10
to rcharlet, golang-nuts
On Thu, Jul 15, 2010 at 2:21 PM, rcharlet <rcha...@speakeasy.net> wrote:
>  But my question to the list is, how should I have known that? I
> can't see anything in the language spec under slices or strings that
> suggest one can post-pend a parenthesized string on a []byte
> declaration. Please either educate me how to read the langage spec or
> update the doc.

It's actually a type conversion. See
http://golang.org/doc/go_spec.html#Conversions

Converting between string and []byte and between string and []int are
special cases in conversion. This is useful because strings are
immutable, but slices are not.

- Evan

jimmy frasche

unread,
Jul 15, 2010, 3:29:43 PM7/15/10
to rcharlet, golang-nuts
http://golang.org/doc/go_spec.html#Conversions it's in the subsection
(that I cannot link directly)
Conversions to and from a string type
Reply all
Reply to author
Forward
0 new messages