dongwu3747 2013-08-06 07:47
浏览 29
已采纳

如何在Go中为内置类型创建别名的文字切片

I have some golang code that manipulates slices of an interface type (Comparable). To test my code, I want to create some fake data and operate on it. However, I'm having trouble doing this in a way that is not incredibly tedious. The only thing I can think to do is create a new type for testing (in this case an alias of type int) that satisfies the Comparable interface, and then feed my tests literal slices of that type. I envision it looking something like the following:

type Comparable interface {
    LT(Comparable) bool
    AsFloat() float64
} 

type testInt int 

func (self testInt) LT(other Comparable) bool { 
    return float64(self) < other.AsFloat() 
} 

func (self testInt) AsFloat() float64 { 
    return float64(self) 
} 

func TestAFunction(t *testing.T) { 
    FunctionToTest([]Comparable{7, 4, 2, 1})
    ....
}

However, with this example, the compiler will complain that type int cannot be used as a Comparable. I understand why this is happening, but I'm not sure how to solve it. First, I don't know how to create a literal of type testInt. Second, I have to write a significant number of these functions. Working with literal ints is far more convenient for my purposes.

Is there a way to work with type aliases of builtin types such that the compiler can correctly infer the correct type of literals with a minimum of code?

Additionally, is there perhaps a better way to accomplish what I am trying to do, i.e., generate hard data that satisfies an interface for use in testing?

  • 写回答

3条回答 默认 最新

  • donglv6747 2013-08-06 14:44
    关注

    There are a number of ways to accomplish this. The problem, as you correctly state, is that the Go compiler cannot automatically convert int to Comparable (since doing so would require finding all possible equivalent types, and figuring out which of those equivalent types satisfy the Comparable interface, and then if there are more than one... you get the idea). Thus, you'll have to do one of two things:

    Write an explicit type conversion:

    FunctionToTest([]Comparable{ testInt(7), testInt(4), testInt(2), testInt(1) })
    

    However, if you need a lot of literals, this could get really annoying. Thus, you could also:

    Write a function to convert []int to []Comparable:

    func intToComparable(i []int) []Comparable {
        c := make([]Comparable, len(i))
        for i, v := range i {
            c[i] = testInt(v)
        }
        return c
    }
    

    and then you'd only have to do:

    FunctionToTest(intToComparable([]int{ 7, 4, 2, 1 }))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题