FAQ
I have a very large array of strings. This is my master list. I have
several workers performing lots of tasks that each loop over that large
array of strings. To ensure that I dont copy my very large array of strings
over to each worker, each worker has a channel of type chan *string. Each
string is my master list is pumped into each workers channel. As far as my
understanding goes, this means that each workers channel is very light as
the dont have a copy of each string, just a reference to the master list.
Each worker then pulls a string pointer from the channel, does some work
(that doesnt modifiy the string) and then moves on to the next job in the
channel.

My problem is this: some of the work requires that I slice the string
returned from the channel, as I want to read say x characters from the
string over a loop. However, I get an error trying to slice *string. What
would you guys recommend as the best way to proceed?

I could copy the string in the worker once its pulled off the channel, but
that kinda feels like cheating and I wondered if there was a better
technique I was missing that wouldnt require me to may a temporary copy of
the string from the master list, as I may have several hundred workers.

Cheers!
Ben

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Jesse McNelis at Oct 24, 2014 at 7:10 am

    On 24 Oct 2014 18:02, wrote:
    I could copy the string in the worker once its pulled off the channel,
    but that kinda feels like cheating and I wondered if there was a better
    technique I was missing that wouldnt require me to may a temporary copy of
    the string from the master list, as I may have several hundred workers.
    >

    A string is a value that contains a pointer to an immutable array of bytes.
    This makes copying and slicing a string cheap as it doesn't require copying
    the byte array and many strings can share the same array.

    Just use a string.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Omarshariffdontlikeit at Oct 24, 2014 at 9:44 am
    Ah-ha! Thanks for the insight guys, didn't know that about strings! That
    means I can ditch a whole bunch of code.
    Cheers,
    Ben

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Kevin Malachowski at Oct 25, 2014 at 7:46 am
    In that case you should read through
    http://blog.golang.org/slices and possibly /strings too if you really want more info.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jan Mercl at Oct 24, 2014 at 7:12 am

    On Fri, Oct 24, 2014 at 9:02 AM, wrote:
    I have a very large array of strings. This is my master list. I have several
    workers performing lots of tasks that each loop over that large array of
    strings. To ensure that I dont copy my very large array of strings over to
    each worker, each worker has a channel of type chan *string.
    Is it really an array or is it a slice? In the later case, passing a
    slice is passing three words only.
    Each string is
    my master list is pumped into each workers channel. As far as my
    understanding goes, this means that each workers channel is very light as
    the dont have a copy of each string, just a reference to the master list.
    Each worker then pulls a string pointer from the channel, does some work
    (that doesnt modifiy the string) and then moves on to the next job in the
    channel.

    My problem is this: some of the work requires that I slice the string
    returned from the channel, as I want to read say x characters from the
    string over a loop. However, I get an error trying to slice *string. What
    would you guys recommend as the best way to proceed?
    Recommendation available. No one can slice a pointer. If want to slice
    the pointee instead:

             a := (*b)[x:y]

    BTW, even though passing around a pointer to a string has its
    legitimate use cases, this seems not to be one of them.

    -j

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jan Mercl at Oct 24, 2014 at 7:13 am
    On Fri, Oct 24, 2014 at 9:11 AM, Jan Mercl wrote:
    s/Recommendation available/Recommendation not available/

    -j

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 24, '14 at 7:02a
activeOct 25, '14 at 7:46a
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase