FAQ
Surprisingly I can't find an example of the idiomatic way to clear a slice.
The following code works, but I'm not sure if it is idiomatic.

letters := []string{"a", "b", "c", "d"}
fmt.Println(fmt.Sprintf ("%d",len(letters)) + " letters.")

letters = letters[:0]
fmt.Println(fmt.Sprintf ("%d",len(letters)) + " letters.")


--
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/groups/opt_out.

Search Discussions

  • Hotei at Apr 21, 2013 at 9:10 pm
    It depends a little bit on what you mean by "clear". From a cryptographic
    viewpoint you're not clearing the memory with the code you propose. (see
    blowfish.go for an example of crypto clear) If another purpose is what you
    have in mind, maybe you are. HOWEVER - If you're not going to re-use the
    variable then there's no point in clearing it. If you are - then just the
    act of assigning it a new value will "clear" it.
    On Sunday, April 21, 2013 5:02:58 PM UTC-4, Dean Schulze wrote:


    Surprisingly I can't find an example of the idiomatic way to clear a
    slice. The following code works, but I'm not sure if it is idiomatic.

    letters := []string{"a", "b", "c", "d"}
    fmt.Println(fmt.Sprintf ("%d",len(letters)) + " letters.")

    letters = letters[:0]
    fmt.Println(fmt.Sprintf ("%d",len(letters)) + " letters.")

    --
    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/groups/opt_out.
  • Dean Schulze at Apr 22, 2013 at 1:29 am
    I don't need to crypto clear the slice.

    I'm using the slice in a loop and the variables accessed by the loop
    populate the slice with different elements. I clear the slice and append
    different elements each iteration.
    On Sunday, April 21, 2013 3:10:29 PM UTC-6, Hotei wrote:

    It depends a little bit on what you mean by "clear". From a cryptographic
    viewpoint you're not clearing the memory with the code you propose. (see
    blowfish.go for an example of crypto clear) If another purpose is what you
    have in mind, maybe you are. HOWEVER - If you're not going to re-use the
    variable then there's no point in clearing it. If you are - then just the
    act of assigning it a new value will "clear" it.
    On Sunday, April 21, 2013 5:02:58 PM UTC-4, Dean Schulze wrote:


    Surprisingly I can't find an example of the idiomatic way to clear a
    slice. The following code works, but I'm not sure if it is idiomatic.

    letters := []string{"a", "b", "c", "d"}
    fmt.Println(fmt.Sprintf ("%d",len(letters)) + " letters.")

    letters = letters[:0]
    fmt.Println(fmt.Sprintf ("%d",len(letters)) + " letters.")

    --
    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/groups/opt_out.
  • Roberto Teixeira at Apr 22, 2013 at 2:00 am
    Can't you simply declare 'letters' inside the loop then? This way it would
    be "cleared" at every interaction.

    If not, then this should clear it, no?

    letters = []string{}

    -rst.
    On Sunday, April 21, 2013, Dean Schulze wrote:

    I don't need to crypto clear the slice.

    I'm using the slice in a loop and the variables accessed by the loop
    populate the slice with different elements. I clear the slice and append
    different elements each iteration.
    On Sunday, April 21, 2013 3:10:29 PM UTC-6, Hotei wrote:

    It depends a little bit on what you mean by "clear". From a
    cryptographic viewpoint you're not clearing the memory with the code you
    propose. (see blowfish.go for an example of crypto clear) If another
    purpose is what you have in mind, maybe you are. HOWEVER - If you're not
    going to re-use the variable then there's no point in clearing it. If you
    are - then just the act of assigning it a new value will "clear" it.
    On Sunday, April 21, 2013 5:02:58 PM UTC-4, Dean Schulze wrote:


    Surprisingly I can't find an example of the idiomatic way to clear a
    slice. The following code works, but I'm not sure if it is idiomatic.

    letters := []string{"a", "b", "c", "d"}
    fmt.Println(fmt.Sprintf ("%d",len(letters)) + " letters.")

    letters = letters[:0]
    fmt.Println(fmt.Sprintf ("%d",len(letters)) + " letters.")


    --
    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 <javascript:_e({},
    'cvml', 'golang-nuts%2Bunsubscribe@googlegroups.com');>.
    For more options, visit https://groups.google.com/groups/opt_out.


    --
    -rst.

    Sent from my mobile.
    Please forgive the spelling and brevity.

    --
    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/groups/opt_out.
  • Hotei at Apr 22, 2013 at 5:42 am
    Dean,
    I'd vote for rst's version as being a little more clear than your original
    - but yours has the better emoticon.
    On Sunday, April 21, 2013 10:00:03 PM UTC-4, Roberto Teixeira wrote:

    Can't you simply declare 'letters' inside the loop then? This way it would
    be "cleared" at every interaction.

    If not, then this should clear it, no?

    letters = []string{}

    -rst.
    On Sunday, April 21, 2013, Dean Schulze wrote:

    I don't need to crypto clear the slice.

    I'm using the slice in a loop and the variables accessed by the loop
    populate the slice with different elements. I clear the slice and append
    different elements each iteration.
    On Sunday, April 21, 2013 3:10:29 PM UTC-6, Hotei wrote:

    It depends a little bit on what you mean by "clear". From a
    cryptographic viewpoint you're not clearing the memory with the code you
    propose. (see blowfish.go for an example of crypto clear) If another
    purpose is what you have in mind, maybe you are. HOWEVER - If you're not
    going to re-use the variable then there's no point in clearing it. If you
    are - then just the act of assigning it a new value will "clear" it.
    On Sunday, April 21, 2013 5:02:58 PM UTC-4, Dean Schulze wrote:


    Surprisingly I can't find an example of the idiomatic way to clear a
    slice. The following code works, but I'm not sure if it is idiomatic.

    letters := []string{"a", "b", "c", "d"}
    fmt.Println(fmt.Sprintf ("%d",len(letters)) + " letters.")

    letters = letters[:0]
    fmt.Println(fmt.Sprintf ("%d",len(letters)) + " letters.")


    --
    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/groups/opt_out.


    --
    -rst.

    Sent from my mobile.
    Please forgive the spelling and brevity.
    --
    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/groups/opt_out.
  • Andrey mirtchovski at Apr 22, 2013 at 5:52 am
    x = x[:0] is much better than x = []T{} because the former will reuse
    the already allocated capacity of the underlying array, while the
    latter will have to do (admittedly smart, but still painfull)
    reallocation while it grows.

    for example: http://play.golang.org/p/XwRaRlJPRC

    --
    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/groups/opt_out.
  • Dawid Polak at Apr 22, 2013 at 12:48 pm
    But x = []T{} is much safer, because side effect of using slice

    for example: http://play.golang.org/p/kpVrAgdgsz




    W dniu poniedziałek, 22 kwietnia 2013 07:52:23 UTC+2 użytkownik andrey
    mirtchovski napisał:
    x = x[:0] is much better than x = []T{} because the former will reuse
    the already allocated capacity of the underlying array, while the
    latter will have to do (admittedly smart, but still painfull)
    reallocation while it grows.

    for example: http://play.golang.org/p/XwRaRlJPRC
    --
    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/groups/opt_out.
  • Kees Varekamp at Apr 22, 2013 at 6:00 am
    Go emoticons. I don't know whether that makes me string[:C] or string[:D]

    --
    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/groups/opt_out.
  • Kees Varekamp at Apr 22, 2013 at 6:02 am
    Go emoticons. I don't know whether that makes me mood[:C] or mood[:D]

    --
    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/groups/opt_out.
  • Yves Junqueira at Apr 22, 2013 at 6:56 am
    Probably not your case but for a byte slice and Read(), you can use
    something like:

    a := make([]byte, max)
    for {
    a = a[:cap(a)] // regrow the slice.
    n, err := input.Read(a)
    // handle error
    a = a[:n]
    // use a.
    }

    This requires no extra allocations and no GC and
    resulted in huge speedups when I started using it.

    In real cases you'll probably use a in another routine so you'll need a
    number of byte slices to work with. I use a buffered channel to pop and
    push them from a free list / memory arena. Works wonderfully.
    On Apr 22, 2013 3:29 AM, "Dean Schulze" wrote:

    I don't need to crypto clear the slice.

    I'm using the slice in a loop and the variables accessed by the loop
    populate the slice with different elements. I clear the slice and append
    different elements each iteration.
    On Sunday, April 21, 2013 3:10:29 PM UTC-6, Hotei wrote:

    It depends a little bit on what you mean by "clear". From a
    cryptographic viewpoint you're not clearing the memory with the code you
    propose. (see blowfish.go for an example of crypto clear) If another
    purpose is what you have in mind, maybe you are. HOWEVER - If you're not
    going to re-use the variable then there's no point in clearing it. If you
    are - then just the act of assigning it a new value will "clear" it.
    On Sunday, April 21, 2013 5:02:58 PM UTC-4, Dean Schulze wrote:


    Surprisingly I can't find an example of the idiomatic way to clear a
    slice. The following code works, but I'm not sure if it is idiomatic.

    letters := []string{"a", "b", "c", "d"}
    fmt.Println(fmt.Sprintf ("%d",len(letters)) + " letters.")

    letters = letters[:0]
    fmt.Println(fmt.Sprintf ("%d",len(letters)) + " letters.")


    --
    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/groups/opt_out.

    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 21, '13 at 9:03p
activeApr 22, '13 at 12:48p
posts10
users7
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase