pointers - 下面的代码如何实现接口(interface)?

标签 pointers go interface

我有以下代码,我想使用接口(interface):

当前代码:

import (        
    "github.com/dorzheh/deployer/ui/dialog_ui"
    . "github.com/dorzheh/go-dialog"
)

// all methods in https://github.com/dorzheh/deployer/blob/master/ui/dialog_ui/dialog_ui.go#L28
type Pb struct {
    sleep time.Duration
    step  int
}

type DialogUi struct {
    *Dialog //The source is https://github.com/dorzheh/go-dialog/blob/master/dialog.go#L34
    Pb *Pb
}

我正在尝试以这种方式实现接口(interface):

import (
    "testing"
    // . "github.com/dorzheh/go-dialog"
    //"github.com/dorzheh/deployer/ui/dialog_ui"
)


type PBifaceT interface {
    Step() int
}

type TestDialogUiT struct {
    Pb *PBifaceT
}

func TestUiValidateUser(t *testing.T) {
    x := dialog_ui.TestDialogUiT{}
    PbPb := ImplPBifaceT{}
    x.Pb = PbPb

    parentId := x.Pb.Step()
    t.Logf(fmt.Sprintf("%v", parentId))
}

我做了一个playground 。正如您所看到的,它运行时出现以下错误:

prog.go:23: cannot use PbPb (type ImplPBifaceT) as type *PBifaceT in assignment:
    *PBifaceT is pointer to interface, not interface
prog.go:25: x.Pb.Step undefined (type *PBifaceT is pointer to interface, not interface)

我尝试将它们转换in this playground :

func NewD() *PBifaceT {
    // var err error
    var res =new(ImplPBifaceT)
    return (*PBifaceT)(res)
}

func main() {
    x := TestDialogUiT{}
    x.Pb = NewD()
    parentId := x.Pb.Step()
    fmt.Sprintf("%v", parentId)

}

问题:

prog.go:23: cannot convert res (type *ImplPBifaceT) to type *PBifaceT
prog.go:30: x.Pb.Step undefined (type *PBifaceT is pointer to interface, not interface)

最佳答案

您确定需要将 Pb 字段设置为 *PBifaceT

如果你把它保留为

type TestDialogUiT struct {
    Pb *PBifaceT
}

你也这么做

x := TestDialogUiT{}

PbPb := ImplPBifaceT{}
x.Pb = PBifaceT(PbPb)

parentId := x.Pb.Step()
fmt.Printf("%v", parentId)

工作正常..

看看这个 playground看看是否有帮助。

我建议你看看这个tutorialthis doc .

我建议您也阅读this SO answer这解释了您不应该使用接口(interface)指针的一些原因。

Background: In Go you pass around a pointer to something because of two reasons:

1) You want because your struct is really large and you want to avoid copying

2) you need to because the calee wants to modify the original (this is typical for methods with a pointer receiver). Now an interface value is really tiny (just two words) so reason 1 to pass a pointer to an interface value does not apply.

Reason 2 does not apply in most cases as passing a pointer to an interface value will allow you to change the interface value itself, but most often you would like to modify the value stored inside the interface value. This value stored inside the interface value often is a pointer value which allows to change the value of a struct by calling methods on an interface value which wrapps a pointer to this struct. This sounds complicated but isn't: The novice Go programmer just doesn't use pointers to interfaces (as this won't do any good) and the experienced Go programmer doesn't use pointers to interfaces (as it won't do much good) unless he needs to modify an interface value, typically during reflection.

关于pointers - 下面的代码如何实现接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38093171/

相关文章:

签名中带有数组类型的 ccall 从 Julia 调用 C 中的结构

c++ - 将第二个指针分配给第一个指针指向的对象

c - 带数组的后增量指针

c - 在 C 中保存和访问指向字符串的指针

c# - 处理与服务定位器模式一起使用时 MongoDB 如何存储 DateTime

java - java接口(interface)中的变量

go - 使用graph-gophers/graphql-go软件包的graphql变异查询错误

go - 使用 golang 在 google admin sdk api 上获取 400 invalid_grant。有什么建议么?

go - 通过 Fyne 的 GUI : changing elements on app page on button clicking

java - 类扩展另一个类并实现 java 中的接口(interface)可以转换为这些类型中的任何一个吗?