FAQ
When sorting (using package "sort.Interface") you need to define "Len",
"Swap" and "Less", but you cannot define them over an slice. Could I define
these operations on the slice type?

If I cannot define them on the slice type, I have to create a new type that
is a slice of my struct. Is there any better approach?

When I receive an Slice of my struct (not the sort-able type, but a simple
slice) I can "transform" it into my sort-able type and sort it.

I have created an example here: http://play.golang.org/p/glmJVz7FTd

But for me it is hard to accept I have to create a new type and a new
variable in order to sort the slice. I expected to be able to tell the
compiler: "Please treat this variable that holds an slice of struct-X, as
if it were this sort-able type that is a slice of struct-X".

I would love to be able to write something like:

- sort.Sort((itemSorter)variableToSort)

OR

- sort.Sort(variableToSort.(itemSorter))

Ain't there any other (more idiomatic) way to do this?

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

  • Egon at Feb 2, 2014 at 8:14 pm
    You can simply cast the items http://play.golang.org/p/oGXIqR6ouD.

    + egon
    On Sunday, February 2, 2014 10:06:26 PM UTC+2, Borja Roux Lorenzo wrote:

    When sorting (using package "sort.Interface") you need to define "Len",
    "Swap" and "Less", but you cannot define them over an slice. Could I define
    these operations on the slice type?

    If I cannot define them on the slice type, I have to create a new type
    that is a slice of my struct. Is there any better approach?

    When I receive an Slice of my struct (not the sort-able type, but a simple
    slice) I can "transform" it into my sort-able type and sort it.

    I have created an example here: http://play.golang.org/p/glmJVz7FTd

    But for me it is hard to accept I have to create a new type and a new
    variable in order to sort the slice. I expected to be able to tell the
    compiler: "Please treat this variable that holds an slice of struct-X, as
    if it were this sort-able type that is a slice of struct-X".

    I would love to be able to write something like:

    - sort.Sort((itemSorter)variableToSort)

    OR

    - sort.Sort(variableToSort.(itemSorter))

    Ain't there any other (more idiomatic) way to do this?
    --
    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.
  • Borja Roux Lorenzo at Feb 2, 2014 at 8:18 pm
    Thanks egon. That is exactly what I was looking for.

    It seems I forgot how to cast. I need some rest.

    El domingo, 2 de febrero de 2014 21:14:15 UTC+1, egon escribió:
    You can simply cast the items http://play.golang.org/p/oGXIqR6ouD.

    + egon
    On Sunday, February 2, 2014 10:06:26 PM UTC+2, Borja Roux Lorenzo wrote:

    When sorting (using package "sort.Interface") you need to define "Len",
    "Swap" and "Less", but you cannot define them over an slice. Could I define
    these operations on the slice type?

    If I cannot define them on the slice type, I have to create a new type
    that is a slice of my struct. Is there any better approach?

    When I receive an Slice of my struct (not the sort-able type, but a
    simple slice) I can "transform" it into my sort-able type and sort it.

    I have created an example here: http://play.golang.org/p/glmJVz7FTd

    But for me it is hard to accept I have to create a new type and a new
    variable in order to sort the slice. I expected to be able to tell the
    compiler: "Please treat this variable that holds an slice of struct-X, as
    if it were this sort-able type that is a slice of struct-X".

    I would love to be able to write something like:

    - sort.Sort((itemSorter)variableToSort)

    OR

    - sort.Sort(variableToSort.(itemSorter))

    Ain't there any other (more idiomatic) way to do this?
    --
    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.
  • Jan Mercl at Feb 2, 2014 at 8:20 pm

    On Sun, Feb 2, 2014 at 9:14 PM, egon wrote:
    You can simply cast the items http://play.golang.org/p/oGXIqR6ouD.
    Simply cast an item in a language which simply doesn't cast? ;-)

    -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/groups/opt_out.
  • Borja Roux Lorenzo at Feb 2, 2014 at 8:32 pm
    Should we say "convert <http://golang.org/ref/spec#Conversions>" instead of
    "cast"?

    If that's the case, I'm not sure about the difference:
    - http://en.wikipedia.org/wiki/Type_conversion
    - http://stackoverflow.com/questions/3166840/what-is-the-difference-between-casting-and-conversion

    I understand there is no implicit type conversion (cannot write var x int =
    1.0), but which would be the right way to name the operation?

    El domingo, 2 de febrero de 2014 21:19:24 UTC+1, Jan Mercl escribió:
    On Sun, Feb 2, 2014 at 9:14 PM, egon <egon...@gmail.com <javascript:>>
    wrote:
    You can simply cast the items http://play.golang.org/p/oGXIqR6ouD.
    Simply cast an item in a language which simply doesn't cast? ;-)

    -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/groups/opt_out.
  • Pierre Durand at Feb 3, 2014 at 9:04 am
    http://golang.org/ref/spec#Type_assertions
    http://golang.org/ref/spec#Conversions


    Le dimanche 2 février 2014 21:32:31 UTC+1, Borja Roux Lorenzo a écrit :
    Should we say "convert <http://golang.org/ref/spec#Conversions>" instead
    of "cast"?

    If that's the case, I'm not sure about the difference:
    - http://en.wikipedia.org/wiki/Type_conversion
    -
    http://stackoverflow.com/questions/3166840/what-is-the-difference-between-casting-and-conversion

    I understand there is no implicit type conversion (cannot write var x int
    = 1.0), but which would be the right way to name the operation?

    El domingo, 2 de febrero de 2014 21:19:24 UTC+1, Jan Mercl escribió:
    On Sun, Feb 2, 2014 at 9:14 PM, egon wrote:
    You can simply cast the items http://play.golang.org/p/oGXIqR6ouD.
    Simply cast an item in a language which simply doesn't cast? ;-)

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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 2, '14 at 8:06p
activeFeb 3, '14 at 9:04a
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase