FAQ
I often write code that uses "tables" of data, which are represented by
slices of slices but each inner slice has the same length. Is there any way
to write a range loop such that it implicitly confirms that each slice is
of the same length without having to actually do length checks? For example,

http://play.golang.org/p/qDmYLAh-UO

will panic if len(s1) < len(s2), but will behave just fine if len(s1) >
len(s2). I'm wondering if I can construct this so it will panic in either
case without increasing runtime computational cost.

Thanks


--
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

  • Martin Angers at Jul 22, 2013 at 8:00 pm
    This is admittedly ugly because of the multiple conversions, but it panics
    in both cases:

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


    Le lundi 22 juillet 2013 15:14:48 UTC-4, Brendan Tracey a écrit :
    I often write code that uses "tables" of data, which are represented by
    slices of slices but each inner slice has the same length. Is there any way
    to write a range loop such that it implicitly confirms that each slice is
    of the same length without having to actually do length checks? For example,

    http://play.golang.org/p/qDmYLAh-UO

    will panic if len(s1) < len(s2), but will behave just fine if len(s1) >
    len(s2). I'm wondering if I can construct this so it will panic in either
    case without increasing runtime computational cost.

    Thanks

    --
    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.
  • Rob Pike at Jul 22, 2013 at 9:08 pm
    Just check the lengths. It's essentially free.

    -rob

    --
    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
postedJul 22, '13 at 7:14p
activeJul 22, '13 at 9:08p
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase