FAQ
Hi all,

I want to implement an array which satisfies the sort.Interface, and the
following code works as my expectation:

package main
import (
"fmt"
"sort"
)
type command struct {
name string
}
type byName [4]command
func (a *byName) Len() int { return len(*a) }
func (a *byName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a *byName) Less(i, j int) bool { return a[i].name < a[j].name }
func main() {
c := byName([...]command{
{"breakpoint"},
{"help"},
{"args"},
{"continue"},
})
fmt.Println("Before sorting: ", c)
sort.Sort(&c)
fmt.Println("After sorting: ", c)
}

Now, I change the main function:
func main() {
c := [...]command{
{"breakpoint"},
{"help"},
{"args"},
{"continue"},
}
fmt.Println("Before sorting: ", c)
sort.Sort(&(byName(c)))
fmt.Println("After sorting: ", c)
}
Then the compiler complains:
src\Hello.go:26: cannot take the address of byName(c)

So except convert "[4]command" to "byName" in the declaration:
c := byName([...]command{
{"breakpoint"},
{"help"},
{"args"},
{"continue"},
})
Is there any method to achieve the same goal?

Thanks in advance!

Best Regards
Nan Xiao

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

  • Dave Cheney at May 25, 2016 at 5:48 am
    Why not use a slice rather than an array ?
    On Wednesday, 25 May 2016 15:38:00 UTC+10, Nan Xiao wrote:

    Hi all,

    I want to implement an array which satisfies the sort.Interface, and the
    following code works as my expectation:

    package main
    import (
    "fmt"
    "sort"
    )
    type command struct {
    name string
    }
    type byName [4]command
    func (a *byName) Len() int { return len(*a) }
    func (a *byName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
    func (a *byName) Less(i, j int) bool { return a[i].name < a[j].name }
    func main() {
    c := byName([...]command{
    {"breakpoint"},
    {"help"},
    {"args"},
    {"continue"},
    })
    fmt.Println("Before sorting: ", c)
    sort.Sort(&c)
    fmt.Println("After sorting: ", c)
    }

    Now, I change the main function:
    func main() {
    c := [...]command{
    {"breakpoint"},
    {"help"},
    {"args"},
    {"continue"},
    }
    fmt.Println("Before sorting: ", c)
    sort.Sort(&(byName(c)))
    fmt.Println("After sorting: ", c)
    }
    Then the compiler complains:
    src\Hello.go:26: cannot take the address of byName(c)

    So except convert "[4]command" to "byName" in the declaration:
    c := byName([...]command{
    {"breakpoint"},
    {"help"},
    {"args"},
    {"continue"},
    })
    Is there any method to achieve the same goal?

    Thanks in advance!

    Best Regards
    Nan Xiao
    --
    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.
  • Frits van Bommel at May 25, 2016 at 5:51 am
    sort.Interface is more commonly defined on slice types than on arrays.

    // ...
    type byName []command
    func (a byName) Len() int { return len(a) }
    func (a byName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
    func (a byName) Less(i, j int) bool { return a[i].name < a[j].name }
    func main() {
    // ...
    fmt.Println("Before sorting: ", c)
    sort.Sort(byName(c[:]))
    fmt.Println("After sorting: ", c)
    }

    (untested, but it should work)
    Note that because you only change the contents, not the len() and cap(),
    you can pass by value and still have it work, so no issues with
    address-taking.


    On Wednesday, May 25, 2016 at 7:38:00 AM UTC+2, Nan Xiao wrote:

    Hi all,

    I want to implement an array which satisfies the sort.Interface, and the
    following code works as my expectation:

    package main
    import (
    "fmt"
    "sort"
    )
    type command struct {
    name string
    }
    type byName [4]command
    func (a *byName) Len() int { return len(*a) }
    func (a *byName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
    func (a *byName) Less(i, j int) bool { return a[i].name < a[j].name }
    func main() {
    c := byName([...]command{
    {"breakpoint"},
    {"help"},
    {"args"},
    {"continue"},
    })
    fmt.Println("Before sorting: ", c)
    sort.Sort(&c)
    fmt.Println("After sorting: ", c)
    }

    Now, I change the main function:
    func main() {
    c := [...]command{
    {"breakpoint"},
    {"help"},
    {"args"},
    {"continue"},
    }
    fmt.Println("Before sorting: ", c)
    sort.Sort(&(byName(c)))
    fmt.Println("After sorting: ", c)
    }
    Then the compiler complains:
    src\Hello.go:26: cannot take the address of byName(c)

    So except convert "[4]command" to "byName" in the declaration:
    c := byName([...]command{
    {"breakpoint"},
    {"help"},
    {"args"},
    {"continue"},
    })
    Is there any method to achieve the same goal?

    Thanks in advance!

    Best Regards
    Nan Xiao
    --
    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.
  • Nan Xiao at May 25, 2016 at 6:01 am
    Hi Dave,

    I find many tutorials are using slice as an example, so just want to try
    another flavor.

    Best Regards
    Nan Xiao
    On Wed, May 25, 2016 at 1:51 PM, Frits van Bommel wrote:

    sort.Interface is more commonly defined on slice types than on arrays.

    // ...
    type byName []command
    func (a byName) Len() int { return len(a) }
    func (a byName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
    func (a byName) Less(i, j int) bool { return a[i].name < a[j].name }
    func main() {
    // ...
    fmt.Println("Before sorting: ", c)
    sort.Sort(byName(c[:]))
    fmt.Println("After sorting: ", c)
    }

    (untested, but it should work)
    Note that because you only change the contents, not the len() and cap(),
    you can pass by value and still have it work, so no issues with
    address-taking.



    On Wednesday, May 25, 2016 at 7:38:00 AM UTC+2, Nan Xiao wrote:

    Hi all,

    I want to implement an array which satisfies the sort.Interface, and the
    following code works as my expectation:

    package main
    import (
    "fmt"
    "sort"
    )
    type command struct {
    name string
    }
    type byName [4]command
    func (a *byName) Len() int { return len(*a) }
    func (a *byName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
    func (a *byName) Less(i, j int) bool { return a[i].name < a[j].name }
    func main() {
    c := byName([...]command{
    {"breakpoint"},
    {"help"},
    {"args"},
    {"continue"},
    })
    fmt.Println("Before sorting: ", c)
    sort.Sort(&c)
    fmt.Println("After sorting: ", c)
    }

    Now, I change the main function:
    func main() {
    c := [...]command{
    {"breakpoint"},
    {"help"},
    {"args"},
    {"continue"},
    }
    fmt.Println("Before sorting: ", c)
    sort.Sort(&(byName(c)))
    fmt.Println("After sorting: ", c)
    }
    Then the compiler complains:
    src\Hello.go:26: cannot take the address of byName(c)

    So except convert "[4]command" to "byName" in the declaration:
    c := byName([...]command{
    {"breakpoint"},
    {"help"},
    {"args"},
    {"continue"},
    })
    Is there any method to achieve the same goal?

    Thanks in advance!

    Best Regards
    Nan Xiao
    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/SI5s9JlQBp8/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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.
  • Val at May 25, 2016 at 1:54 pm
    Hello Xiao

    It is totally legit to use sort.Sort on an array type. The sort package
    has no idea whether the backing structure is slice, or array, or real goods
    wagons, it just needs to have Len/Swap/Less to play with.

    For some reason, you can take the address of a variable, but not of a func
    result or conversion result :

    https://play.golang.org/p/FpVPfOkyX7
    https://play.golang.org/p/70lMhVQ-U-
    https://play.golang.org/p/Mpg4Chg8Nj
    https://play.golang.org/p/cOoaQve5Zh
    https://play.golang.org/p/FgI_b3GyTJ

    Here is a spec section <https://golang.org/ref/spec#Address_operators>
    about what you may take address of. It is related to the concept of
    "addressability". There also exist some old discussions on golang-nuts and
    on stackoverflow about "why can't I taka address of func call" or similar.

    Cheers
      Val
    On Wednesday, May 25, 2016 at 8:01:39 AM UTC+2, Nan Xiao wrote:

    Hi Dave,

    I find many tutorials are using slice as an example, so just want to try
    another flavor.

    Best Regards
    Nan Xiao

    On Wed, May 25, 2016 at 1:51 PM, Frits van Bommel <fvbo...@gmail.com
    <javascript:>> wrote:
    sort.Interface is more commonly defined on slice types than on arrays.

    // ...
    type byName []command
    func (a byName) Len() int { return len(a) }
    func (a byName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
    func (a byName) Less(i, j int) bool { return a[i].name < a[j].name }
    func main() {
    // ...
    fmt.Println("Before sorting: ", c)
    sort.Sort(byName(c[:]))
    fmt.Println("After sorting: ", c)
    }

    (untested, but it should work)
    Note that because you only change the contents, not the len() and cap(),
    you can pass by value and still have it work, so no issues with
    address-taking.



    On Wednesday, May 25, 2016 at 7:38:00 AM UTC+2, Nan Xiao wrote:

    Hi all,

    I want to implement an array which satisfies the sort.Interface, and the
    following code works as my expectation:

    package main
    import (
    "fmt"
    "sort"
    )
    type command struct {
    name string
    }
    type byName [4]command
    func (a *byName) Len() int { return len(*a) }
    func (a *byName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
    func (a *byName) Less(i, j int) bool { return a[i].name < a[j].name }
    func main() {
    c := byName([...]command{
    {"breakpoint"},
    {"help"},
    {"args"},
    {"continue"},
    })
    fmt.Println("Before sorting: ", c)
    sort.Sort(&c)
    fmt.Println("After sorting: ", c)
    }

    Now, I change the main function:
    func main() {
    c := [...]command{
    {"breakpoint"},
    {"help"},
    {"args"},
    {"continue"},
    }
    fmt.Println("Before sorting: ", c)
    sort.Sort(&(byName(c)))
    fmt.Println("After sorting: ", c)
    }
    Then the compiler complains:
    src\Hello.go:26: cannot take the address of byName(c)

    So except convert "[4]command" to "byName" in the declaration:
    c := byName([...]command{
    {"breakpoint"},
    {"help"},
    {"args"},
    {"continue"},
    })
    Is there any method to achieve the same goal?

    Thanks in advance!

    Best Regards
    Nan Xiao
    --
    You received this message because you are subscribed to a topic in the
    Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/golang-nuts/SI5s9JlQBp8/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to
    golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/d/optout.
    --
    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.
  • Luzon83 at May 25, 2016 at 4:27 pm

    On Wednesday, May 25, 2016 at 3:54:11 PM UTC+2, Val wrote:
    The sort package has no idea whether the backing structure is slice, or
    array, or real goods wagons
    It can even sort a uint32: https://play.golang.org/p/0em00MxRJz (code by
    @davecheney)

    --
    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
postedMay 25, '16 at 5:38a
activeMay 25, '16 at 4:27p
posts6
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase