登录
首页 >  Golang >  Go问答

无法建立模拟Google Cloud Storage接口

来源:stackoverflow

时间:2024-03-29 16:48:29 318浏览 收藏

积累知识,胜过积蓄金银!毕竟在Golang开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《无法建立模拟Google Cloud Storage接口》,就带大家讲解一下知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

我正在尝试创建一个接口来抽象谷歌云存储。

我有以下接口:

type client interface {
    bucket(name string) *buckethandle
    close() error
}

type buckethandle interface {
    create(context.context, string, *storage.bucketattrs) error
    delete(context.context) error
    attrs(context.context) (*storage.bucketattrs, error)
}

还有我的代码

type bucket struct {
    handler client
}

func newstorage(ctx context.context, bucketname string) bucket {
    var bkt bucket
    client, err := storage.newclient(ctx)
    if err != nil {
        return bucket{}
    }

    bkt.handler = client
    return bkt
}

我收到以下错误:无法使用客户端(*storage.client 类型的变量)作为赋值中的客户端值:方法 bucket 的类型错误

goland 显示以下内容

Cannot use 'client' (type *Client) as type Client Type does not implement 'Client' need method: Bucket(name string) *BucketHandle have method: Bucket(name string) *BucketHandle

我不明白为什么类型不同。


解决方案


这个错误没有任何问题。它看起来具有误导性的原因是因为您创建了一个与库中的具体结构名称完全相同的接口,即 buckethandle

密切注意两个函数中返回类型之间的差异:

// in your interface, the return type is an interface that you created
bucket(name string) *buckethandle

// in the library, the return type is a concrete struct that exists in that lib
bucket(name string) *buckethandle

您需要将 client 接口修改为以下内容,它应该可以正常工作。

type Client interface {
    Bucket(name string) *storage.BucketHandle
    Close() error
}

今天关于《无法建立模拟Google Cloud Storage接口》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>