小编Mag*_*ama的帖子

如何实现通用接口?

我刚刚看到 Go 在其最新版本中合并了泛型,我正在尝试创建一个小项目来了解它是如何工作的。除了现在通用的非常简单的功能之外,我似乎不知道它是如何工作的。我希望能够做这样的事情:

type Dao[RT any] interface {
    FindOne(id string) *RT
}

type MyDao struct {
}

type ReturnType struct {
    id int
}

func (m *MyDao) FindOne(id string) *ReturnType {
    panic("implement me")
}

// how should this look like?

func NewMyDao() *Dao[ReturnType] {
    return &MyDao[ReturnType]{}
}
Run Code Online (Sandbox Code Playgroud)

这可能吗?我似乎没有以这种方式实现接口,并且我已经尝试了许多相同的组合。

有没有办法实现通用接口?如果不是,是否只能返回类型interface{}

generics interface go

34
推荐指数
2
解决办法
5万
查看次数

SingleThreadExecutor VS普通线程

除了Executor接口比普通线程(例如管理)具有一些优势之外,在执行以下操作之间是否存在任何真正的内部差异(大的性能差异,资源消耗......)

ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(runnable);
Run Code Online (Sandbox Code Playgroud)

和:

Thread thread = new Thread(runnable);
thread.start();
Run Code Online (Sandbox Code Playgroud)

我这里只询问一个帖子.

java multithreading threadpoolexecutor

16
推荐指数
2
解决办法
4049
查看次数