Interface 类型上的Golang接口

Interface 类型上的Golang接口,interface,struct,go,Interface,Struct,Go,我是新手,我正在使用golang编写一个简单的类型接口。 该类型定义为: type Sequence []float64 and the interface is: type Stats interface { greaterThan(x float64) Sequence } 函数greaterThanx float64应返回与对象中的数字相同的新序列 //除非所有小于或等于x的数字都已删除 这是我的尝试,但它不会编译。我不知道怎么修理它。 我的问题是:如何从结构类型中

我是新手,我正在使用golang编写一个简单的类型接口。 该类型定义为:

type Sequence []float64

and the interface is:

type Stats interface {

        greaterThan(x float64) Sequence
}
函数greaterThanx float64应返回与对象中的数字相同的新序列 //除非所有小于或等于x的数字都已删除

这是我的尝试,但它不会编译。我不知道怎么修理它。 我的问题是:如何从结构类型中删除项?我应该用地图吗?作为我的尝试

package main

import "fmt"

type Sequence []float64

type Stats interface {

        greaterThan(x float64) Sequence
}

func (s Sequence) greaterThan(x float64) Sequence{

    var i int
    var f float64
    set := make(map[float64]int)
    var v = f[i] Sequence

    for i, f := range set{

    for j := 0; j <= len(s); j++ {
        if s[j] <= x {
        delete(set, s[j])
        }
    }
}

    return v
}

func display(s Sequence) {

        fmt.Println("s.greaterThan(2):", s.greaterThan(2))

}

func main() {

        s := Sequence([]float64{1, 2, 3, -1, 6, 3, 2, 1, 0})
        display(s)

}
我会这样做:

package main
import "fmt"
type Sequence []float64
type Stats interface {
    greaterThan(x float64) Sequence
}

func (s Sequence) greaterThan(x float64) (ans Sequence) {
    for _, v := range s {
        if v > x {
            ans = append(ans, v)
        }
    }
    return ans
}

func main() {
    s := Sequence{1, 2, 3, -1, 6, 3, 2, 1, 0}
    fmt.Printf("%v\n", s.greaterThan(2))
}

最可能的情况是,您不应该从切片中删除项目,而应该构建一个只包含所需项目的新项目


出于好奇:您想对接口Stat做什么?

编译尝试向您显示了什么错误消息?顺便说一句。如图所示,delete将案例集中的映射和案例j中的键作为参数。所以delete调用看起来像deleteset,我不是deleteset,s[j]