time.Now() 和 time.Now().Local() 有什么区别?

我在 golang 插件模块中有以下代码:


即插即用


package main


import "fmt"


var (

    Thing        = New("first thing")

    ThingFactory = thingFactory{}

)


type thing struct {

    i int

    s string

}


func New(s string) thing {

    return thing{s: s}

}


func (t *thing) Say() string {

    t.i++

    return fmt.Sprintf("%s - %d", t.s, t.i)

}


type thingFactory struct{}


func (t thingFactory) Make(s string) thing {

    return New(s)

}

它被编译为 .so 对象并在另一个程序中使用:


主程序


package main


import (

    "fmt"

    "plugin"

)


func main() {

    p, err := plugin.Open("../plug/plug.so")

    if err != nil {

        panic(err)

    }

    symbol, err := p.Lookup("Thing")

    if err != nil {

        panic(err)

    }

    thing := symbol.(Sayer)

    fmt.Println(thing.Say())


    symbol, err = p.Lookup("ThingFactory") // <-problems start here

    if err != nil {

        panic(err)

    }

    factory := symbol.(GetSayer)


    madeThing := factory.Make("how about me?")

    fmt.Println(madeThing.Say())

    fmt.Println(madeThing.Say())

}


type Sayer interface {

    Say() string

}


type GetSayer interface {

    Make(string) Sayer

}

我能够查找Thing, 并调用Say()它,但是第二个接口转换出现混乱:


first thing - 1

panic: interface conversion: *main.thingFactory is not main.GetSayer: missing method Make


即使运行时将第一个符号识别为 a,Sayer它也没有识别出thingFactory显然有一个 Make() 方法,该方法应该返回同样是 Sayer 的东西。


我在这里遗漏了一些明显的东西吗?


小唯快跑啊
浏览 171回答 2
2回答

万千封印

第一个问题是在你的插件中thingFactory(更准确地说*thingfactory)没有在你的主应用程序GetSayer界面中描述的方法:Make(string) Sayer你有:Make(string) thing所以(首先)你必须改成thingFactory.Make()这样:type Sayer interface {&nbsp; &nbsp; Say() string}func (t thingFactory) Make(s string) Sayer {&nbsp; &nbsp; th := New(s)&nbsp; &nbsp; return &th}在此之后它仍然无法正常工作。这是因为插件的Sayer类型与您的主应用程序的类型不同Sayer。但它们必须相同才能实现您的主应用程序GetSayer界面。一种解决方案是将接口“外包”Sayer给它自己的包,并在插件和主应用程序中使用这个通用的共享包。让我们创建一个新包,将其命名为subplay:package subplaytype Sayer interface {&nbsp; &nbsp; Say() string}导入这个包并在插件中使用它:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "path/to/subplay")var (&nbsp; &nbsp; Thing&nbsp; &nbsp; &nbsp; &nbsp; = New("first thing")&nbsp; &nbsp; ThingFactory = thingFactory{})type thing struct {&nbsp; &nbsp; i int&nbsp; &nbsp; s string}func New(s string) thing {&nbsp; &nbsp; return thing{s: s}}func (t *thing) Say() string {&nbsp; &nbsp; t.i++&nbsp; &nbsp; return fmt.Sprintf("%s - %d", t.s, t.i)}type thingFactory struct{}func (t thingFactory) Make(s string) subplay.Sayer {&nbsp; &nbsp; th := New(s)&nbsp; &nbsp; return &th}并在主应用程序中导入和使用它:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "path/to/subplay"&nbsp; &nbsp; "plugin")func main() {&nbsp; &nbsp; p, err := plugin.Open("../plug/plug.so")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; symbol, err := p.Lookup("Thing")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; thing := symbol.(subplay.Sayer)&nbsp; &nbsp; fmt.Println(thing.Say())&nbsp; &nbsp; symbol, err = p.Lookup("ThingFactory")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; factory := symbol.(GetSayer)&nbsp; &nbsp; madeThing := factory.Make("how about me?")&nbsp; &nbsp; fmt.Println(madeThing.Say())&nbsp; &nbsp; fmt.Println(madeThing.Say())}type GetSayer interface {&nbsp; &nbsp; Make(string) subplay.Sayer}现在它可以工作了,输出将是:first thing - 1how about me? - 1how about me? - 2

蝴蝶刀刀

你的插件 Make 方法应该返回一个 Sayer 对象而不是东西type Sayer interface {&nbsp; &nbsp; Say() string}func (t *thingFactory) Make(s string) Sayer {&nbsp; &nbsp; return New(s)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go