Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 排序。在Go中反转_Sorting_Go_Interface - Fatal编程技术网

Sorting 排序。在Go中反转

Sorting 排序。在Go中反转,sorting,go,interface,Sorting,Go,Interface,我正在查看排序。反向代码: type reverse struct { // This embedded Interface permits Reverse to use the methods of // another Interface implementation. Interface } // Less returns the opposite of the embedded implementation's Less method. func (r reve

我正在查看排序。反向代码:

type reverse struct {
    // This embedded Interface permits Reverse to use the methods of
    // another Interface implementation.
    Interface
}

// Less returns the opposite of the embedded implementation's Less method.
func (r reverse) Less(i, j int) bool {
    return r.Interface.Less(j, i)
}

// Reverse returns the reverse order for data.
func Reverse(data Interface) Interface {
    return &reverse{data}
}
据我所知,
reverse
实现了
Interface
(并覆盖
Less
),而
*reverse
不实现
Interface
。为什么
Reverse
返回
*Reverse
(它在某种程度上是
接口
)?

请注意:

据我所知,反向实现接口(和重写 减去)

Golang中没有方法重写。它没有覆盖
Less
。它正在实现
Interface
Less
,这是一种
接口
类型:

type Interface interface {
        // Len is the number of elements in the collection.
        Len() int
        // Less reports whether the element with
        // index i should sort before the element with index j.
        Less(i, j int) bool
        // Swap swaps the elements with indexes i and j.
        Swap(i, j int)
}
就所提出的问题:

为什么Reverse返回*Reverse(某种程度上是接口)

因为要更改数据的顺序,它应该是反向结构的地址。这就是为什么它是反向的。现在您可以传递实现
接口
接口的任何类型。为了实现
接口
,您应该实现
接口
中定义的所有方法

type IntSlice []int

func (p IntSlice) Len() int           { return len(p) }
func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p IntSlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

:“对应指针类型*T的方法集是用receiver*T或T声明的所有方法的集合(也就是说,它也包含方法集T)。”因此*reverse也有reverse的方法。啊哈,我知道了。我认为*T和T总是有不重叠的方法集。如果你把这个评论扩大到一个答案,我会接受的。谢谢@Marii no*T和T是不同类型的接收器。不能传递两个名称相同且类型为T或*T的方法。它将显示一个错误。请检查此链接这是一个非常好的问题。我希望会有更多的投票。为了获得完整的答案,我推荐关于从有效go嵌入的章节:
package main

import (
    "fmt"
    "sort"
)

func main() {
    //s := []int{5, 2, 6, 3, 1, 4} // unsorted
    s1 := []float64{5.2, 2.6, .6, .03, 2.1 } // unsorted
    sort.Sort(sort.Reverse(sort.Float64Slice(s1)))
    fmt.Println(s1)
}