FAQ
Hi,
I am now trying for some time to convert a slice of strings in a slice of
bytes but it still not work.
I guess i need some help ...

data := make([]byte, len(u.DeviceIds))
for _, deviceId := range u.DeviceIds {
data = append(data, []byte(deviceId))
}

This is my idea ... compiling fails and it not working ...

Greets,
Moddus

--

Search Discussions

  • Dave Cheney at Dec 16, 2012 at 12:04 am
    append has the signature, T = append(T, t), where T is a slice of t's.
    So in your example what you have is

    var data []byte
    var d []byte
    data = append(data, d)

    this is incorrect because appending a []byte, to data, would imply
    that its type is [][]byte.

    To solve this use the varargs ... form

    data = append(data, d...)

    http://golang.org/ref/spec#Appending_and_copying_slices

    Cheers

    Dave
    On Sun, Dec 16, 2012 at 10:58 AM, Moddus wrote:
    Hi,
    I am now trying for some time to convert a slice of strings in a slice of
    bytes but it still not work.
    I guess i need some help ...

    data := make([]byte, len(u.DeviceIds))
    for _, deviceId := range u.DeviceIds {
    data = append(data, []byte(deviceId))
    }

    This is my idea ... compiling fails and it not working ...

    Greets,
    Moddus

    --
    --
  • Kevin Gillette at Dec 16, 2012 at 12:55 am
    Additionally, initializing data to len(u.DeviceIds) is probably not
    helpful. If there are 20 deviceids, with an average individual length of,
    say, 10, then that's ~200 bytes (though in this case, you would have
    allocated 20 bytes), and because you're using the 2-arg form of make,
    you're setting the initial _len_ not the initial cap of data, and append
    only fills in the amount of cap that extends beyond the len. What this
    means, is is you have 20 device ids, then your resulting data slice will
    start with 20 null bytes.

    If you can reasonably determine the capacity you need, do:

    data := make([]byte, 0, neededCap)

    Otherwise, don't worry about it, and just don't initialize data at all:

    var data []byte

    append will always allocate more space (for the new slice it returns) as
    needed.
    On Saturday, December 15, 2012 4:58:23 PM UTC-7, Moddus wrote:

    Hi,
    I am now trying for some time to convert a slice of strings in a slice of
    bytes but it still not work.
    I guess i need some help ...

    data := make([]byte, len(u.DeviceIds))
    for _, deviceId := range u.DeviceIds {
    data = append(data, []byte(deviceId))
    }

    This is my idea ... compiling fails and it not working ...

    Greets,
    Moddus
    --
  • Minux at Dec 16, 2012 at 12:59 am

    On Sun, Dec 16, 2012 at 7:58 AM, Moddus wrote:

    I am now trying for some time to convert a slice of strings in a slice of
    bytes but it still not work.
    I guess i need some help ...

    data := make([]byte, len(u.DeviceIds))
    for _, deviceId := range u.DeviceIds {
    data = append(data, []byte(deviceId))
    }

    This is my idea ... compiling fails and it not working ...
    I wonder what are you trying to do?

    do you want to convert a slice of string to a slice of byte slices?
    or concatenate a slice of string into a single byte slice?

    --
  • Moddus at Dec 16, 2012 at 10:45 am
    Thanks for your replies.
    I want to concatenate a slice of string into a single byte slice.
    So like Kevin explains this should work:

    func main() {
    ids := []string{"Foo","Bar","FooBar"}
    data := make([]byte,0)
    for _, id := range ids {
    data = append(data, id...)
    }
    fmt.Println(len(data), cap(data), string(data))
    }

    Is this solution a acceptable way ? Or should i do something different ?

    Cheers,
    Moddus


    Am Sonntag, 16. Dezember 2012 00:58:23 UTC+1 schrieb Moddus:
    Hi,
    I am now trying for some time to convert a slice of strings in a slice of
    bytes but it still not work.
    I guess i need some help ...

    data := make([]byte, len(u.DeviceIds))
    for _, deviceId := range u.DeviceIds {
    data = append(data, []byte(deviceId))
    }

    This is my idea ... compiling fails and it not working ...

    Greets,
    Moddus
    --
  • Minux at Dec 16, 2012 at 2:46 pm

    On Sun, Dec 16, 2012 at 6:45 PM, Moddus wrote:

    Thanks for your replies.
    I want to concatenate a slice of string into a single byte slice.
    So like Kevin explains this should work:

    func main() {
    ids := []string{"Foo","Bar","FooBar"}
    data := make([]byte,0)
    if len == 0, you don't need to write it out, just write data :=
    make([]byte)
    for _, id := range ids {
    data = append(data, id...)
    }
    fmt.Println(len(data), cap(data), string(data))
    }

    Is this solution a acceptable way ? Or should i do something different ?
    yes, it is.

    --
  • Steven Blenkinsop at Dec 16, 2012 at 9:08 pm

    On Sunday, December 16, 2012, minux wrote:
    if len == 0, you don't need to write it out, just write data :=
    make([]byte)
    That doesn't work with slices. The options are:

    data := make([]byte, 0)
    or
    data := []byte{}

    These first two are essentially equivalent, though the first might be
    handled at run time and the second statically at compile time (I'm not
    sure, though), and the second one is shorter too, obviously. In this
    context, though, you don't actually need a non-nil slice, so either of
    these should do:

    var data []byte
    or
    data := []byte(nil)

    A final option is to allocate a slice with sufficient capacity from the
    start:

    data := make([]byte, 0, N)

    You can calculate N from the slice of strings before allocating the byte
    slice. This will most likely cost less than dynamically growing the slice
    as you go.

    --
  • Minux at Dec 16, 2012 at 9:21 pm

    On Monday, December 17, 2012, Steven Blenkinsop wrote:
    On Sunday, December 16, 2012, minux wrote:


    if len == 0, you don't need to write it out, just write data :=
    make([]byte)
    That doesn't work with slices. The options are:

    data := make([]byte, 0)
    or
    data := []byte{}
    oops, sorry. thank you for pointing out.
    These first two are essentially equivalent, though the first might be
    handled at run time and the second statically at compile time (I'm not
    sure, though), and the second one is shorter too,
    i think you are correct here regarding the current implementation,
    make (runtime.makeslice) will always be called regardless of the value
    of its argument.
    obviously. In this context, though, you don't actually need a non-nil
    slice, so either of these should do:

    var data []byte
    or
    data := []byte(nil)

    A final option is to allocate a slice with sufficient capacity from the
    start:

    data := make([]byte, 0, N)

    You can calculate N from the slice of strings before allocating the byte
    slice. This will most likely cost less than dynamically growing the slice
    as you go.
    --
  • Matt Harden at Dec 16, 2012 at 3:31 pm

    On Sunday, December 16, 2012 4:45:21 AM UTC-6, Moddus wrote:
    Thanks for your replies.
    I want to concatenate a slice of string into a single byte slice.
    So like Kevin explains this should work:

    func main() {
    ids := []string{"Foo","Bar","FooBar"}
    data := make([]byte,0)
    for _, id := range ids {
    data = append(data, id...)
    }
    fmt.Println(len(data), cap(data), string(data))
    }

    Is this solution a acceptable way ? Or should i do something different ?
    It's certainly acceptable, but may I suggest using the standard library:

    data := []byte(strings.Join(ids, ""))

    http://play.golang.org/p/idWuGP9H83

    --
  • Kevin Gillette at Dec 17, 2012 at 12:08 am

    On Sunday, December 16, 2012 8:31:33 AM UTC-7, Matt Harden wrote:

    It's certainly acceptable, but may I suggest using the standard library:

    data := []byte(strings.Join(ids, ""))

    http://play.golang.org/p/idWuGP9H83
    Agreed, for this particular problem, strings.Join is definitely the best
    approach. It's easy to forget that many mailing list questions have simple
    solutions.

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 15, '12 at 11:58p
activeDec 17, '12 at 12:08a
posts10
users6
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase