相关疑难解决方法(0)

确保类型在Go中的编译时实现接口

如何确保类型在编译时实现接口?执行此操作的典型方法是无法从该类型分配支持接口,但是我有几种仅动态转换的类型.在运行时,这会生成非常粗暴的错误消息,而没有为编译时错误提供更好的诊断.在运行时找到我希望支持接口的类型也很不方便,事实上并非如此.

types compiler-errors interface go

15
推荐指数
3
解决办法
4270
查看次数

golang接口合规性编译类型检查

我从camlistore看到以下声明(http://code.google.com/p/camlistore/source/browse/pkg/cacher/cacher.go).

var (
        _ blobref.StreamingFetcher = (*CachingFetcher)(nil)
        _ blobref.SeekFetcher      = (*CachingFetcher)(nil)
        _ blobref.StreamingFetcher = (*DiskCache)(nil)
        _ blobref.SeekFetcher      = (*DiskCache)(nil)
)
Run Code Online (Sandbox Code Playgroud)

我知道没有创建变量,并且语句确保编译器检查CachingFether实现StreamingFetcher和SeekFetcher的公共函数.RHS部分使用带有nil参数的指针构造函数语法.这种语法在Go语言中意味着什么?

pointers interface go

5
推荐指数
1
解决办法
1736
查看次数

检查变量实现接口而不编译

我想知道具体类型是否实现了一个specefic接口并将其打印出来.我编写了一个带有自定义结构(MyPoint)的示例[0],而不是接口类型.MyPoint具有io.Reader接口中定义的Read函数:

type MyPoint struct {
   X, Y int
}

func (pnt *MyPoint) Read(p []byte) (n int, err error) {
    return 42, nil
}
Run Code Online (Sandbox Code Playgroud)

目的是获取具体类型p实现接口io.Writer的信息.因此,我写了一个简短的主要内容来获得支票.

func main() {
     p := MyPoint{1, 2}
}
Run Code Online (Sandbox Code Playgroud)

第一个想法是在反射和类型开关的帮助下检查它并添加check(p)到主函数.

func checkType(tst interface{}) {
    switch tst.(type) {
    case nil:
        fmt.Printf("nil")
    case *io.Reader:
        fmt.Printf("p is of type io.Reader\n")
    case MyPoint:
        fmt.Printf("p is of type MyPoint\n")
    default:
        fmt.Println("p is unknown.")
    }
}
Run Code Online (Sandbox Code Playgroud)

输出是:p is of type MyPoint.经过一些研究,我知道我应该预料到,因为Go的类型是静态的,因此p的类型是MyPoint而不是io.Reader.除此之外,io.Reader是一种与MyPoint类型不同的接口类型.

我在[1]找到了一个解决方案,它检查MyPoint在编译时是否可以是io.Reader.有用.

var _ io.Reader = (*MyPoint)(nil)
Run Code Online (Sandbox Code Playgroud)

但这不是我想要的解决方案.下面的尝试也失败了.我认为这是因为上面的原因,不是吗?

i …
Run Code Online (Sandbox Code Playgroud)

reflection interface go

4
推荐指数
2
解决办法
2521
查看次数

在struct上使用interface {}来检查它是否满足golang中的接口

给出以下代码:

package main

import (
    "fmt"
)

type work interface {
    filter() bool
}

type organ struct {
    name string
}

func (s *organ) filter () bool {
    return true;
}


func main() {
    kidney := &organ {
            name : "kidney",    
        }

    _, ok := interface{}(kidney).(work)
    fmt.Println(ok);
}
Run Code Online (Sandbox Code Playgroud)

我没有完全得到以下部分:

_, ok := interface{}(kidney).(work)
Run Code Online (Sandbox Code Playgroud)

在我看来,它是将结构转换为interface{}类型,我理解,但为什么需要转换为interface{}类型来检查它是否满足另一个接口.更具体地说,为什么以下代码失败?

ok := kidney.(work)
Run Code Online (Sandbox Code Playgroud)

有错误

invalid type assertion: kidney.(work) (non-interface type *organ on left)

interface go

4
推荐指数
1
解决办法
1295
查看次数

如何检查值是否实现了接口

我想通过具体方式比较我的类型.为此,我MyType.Same(other MyType) bool为每种类型创建了函数.

在一些泛型函数中,我想检查参数是否具有"Same"函数,如果是则调用它.

如何针对不同类型以通用方式执行此操作?

type MyType struct {
   MyField string
   Id string // ignored by comparison
}

func (mt MyType) Same(other MyType) bool {
    return mt.MyField == other.MyField
}

// MyOtherType... Same(other MyOtherType)


type Comparator interface {
    Same(Camparator) bool // or Same(interface{}) bool
}

myType = new(MyType)
_, ok := reflect.ValueOf(myType).Interface().(Comparator) // ok - false

myOtherType = new(myOtherType)
_, ok := reflect.ValueOf(myOtherType).Interface().(Comparator) // ok - false
Run Code Online (Sandbox Code Playgroud)

go

1
推荐指数
1
解决办法
1119
查看次数

标签 统计

go ×5

interface ×4

compiler-errors ×1

pointers ×1

reflection ×1

types ×1