dooid3005 2018-11-04 17:18
浏览 49
已采纳

为什么我得到一个引用接口的空变量?

package main

import (
    "fmt"
    "net/http"
    "sync"
    "time"
)

type myInterface interface {
    doFunc() bool
}

type myStruct struct {
    myValue string
    si            myInterface 
}

func newStrcut(si myInterface ) *myStruct {
    return &myStruct {si: si}
}

var myS *myStruct 

func main() {

    myS = newStrcut(&newStrcut{})
    myS.myValue = "test"
    if myS.doMyLogic() {
        return
    }

}

func (s *myStruct) doMyLogic() bool {
    fmt.Printf(s.myValue )
    s.si.doFunc()
    return false
}

func (s *myStruct) doFunc() bool {
    fmt.Printf(s.myValue)
    return false
}

Why do I get different values for s.MyValue in doFunc and doMyLogic? In doMyLogic it was test and in doFunc it is "".

  • 写回答

2条回答 默认 最新

  • douxie2007 2018-11-04 17:58
    关注

    Jeremy covered why it's not working. I think I can answer what the OP wants to do. I think there's a misunderstanding about how interfaces work. I think the OP is trying to give myStruct the myInterface interface, but interfaces are implicit.

    type myInterface interface {
        doFunc() bool
    }
    

    This makes an interface myInterface. Things do not need to be declared to be of an interface. Anything which satisfies the interface is that interface. Anything which defines doFunc() bool is a myInterface. There's no need to declare it.

    I want that the myValue will be the same for the same objects in the same structure – user1365697 15 mins ago

    The idea is to create interface and then to use it from the tests and call to the same method that create the myValue and then send the relevant structure – user1365697 13 mins ago OP in comment

    type myStruct struct {
        myValue string
        si            myInterface 
    }
    

    This defines a struct which has a string, and it also has something which implements myInterface. Again, that's anything which defines doFunc() bool.

    It's important to realize that myStruct.si is a totally different thing with (potentially, because it's not defined) its own myValue.

    Instead of giving myStruct its own extra struct, I think what the OP intended is to give myStruct the myInterface interface. Since interfaces are implicit myStruct already satisfies myInterface. It should look like this:

    package main
    
    import (
        "fmt"
    )
    
    type myInterface interface {
        doFunc() bool
    }
    
    type myStruct struct {
        myValue string
    }
    
    func (s myStruct) doMyLogic() bool {
        fmt.Println(s.myValue)
        s.doFunc()
        return false
    }
    
    func (s myStruct) doFunc() bool {
        fmt.Println(s.myValue)
        return false
    }
    
    func main() {
        myS := myStruct{ myValue: "test" }
        if myS.doMyLogic() {
            return
        }
    }
    

    Because myStruct has doFunc() bool defined it satisfies the myInterface interface. There's no need to add an extra field for it.

    You can pass myStruct to anything requiring myInterface. That's the idea behind interfaces. Rather than making explicit declarations, anything which satisfies the interface will work. Like Duck Typing, but strict.

    func usesMyInterface(s myInterface) bool {
        return s.doFunc()
    }
    
    func main() {
        myS := myStruct{ myValue: "test" }
        usesMyInterface(myS)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么