<p>This might be a dumb question, but I have the following code</p>
<pre><code>package SomeCleverDistributedAlgorithm
type AbstractServer interface {
doSomething(string) interface{}
}
type AlgorithmStruct struct {
}
func NewSomeCleverDistributedAlgorithm(serverList []AbstractServer) *AlgorithmStruct {
return &AlgorithmStruct{}
}
</code></pre>
<p>and</p>
<pre><code>package serverImpl
type FakeServer struct {
id int
}
func NewFakeServer() *FakeServer {
return &FakeServer{}
}
func (server *FakeServer) doSomething(key string) interface{} {
return ""
}
</code></pre>
<p>What I found is that a list of <code>FakeServer</code> is not the same as a list of <code>AbstractServer</code> when I try to use <code>FakeServer</code> to initialize <code>NewSomeCleverDistributedAlgorithm</code>, as the compiler give the following error</p>
<pre><code> have serverImpl.processQueryByKey(string) interface {}
want SomeCleverDistributedAlgorithm.processQueryByKey(string) interface {}
</code></pre>
<p>I try fiddling around and could not get the logic I want to work (eg. Have another package implementing the interface <code>AbstractServer</code>). </p>
<p>Can someone help me out?</p>
<p>Thanks</p>
<hr/>**评论:**<br/><br/>jiimji: <pre><p>Welcome to Go ... </p>
<blockquote>
<p>What I found is that a list of FakeServer is not the same as a list of AbstractServer</p>
</blockquote>
<p>2 slices of 2 interface types that are "equivalent" == 2 different types that are not compatible. The solution is to manually convert the first slice of interface A into an slice of interface B with a for loop. </p>
<p>You can also put FakeServer values in an array of AbstractServer </p>
<p>edit : clarity</p></pre>wefnpwfpawfema: <pre><p>hmm, interesting, when we say manually convert do we mean</p>
<pre><code>SomeCleverDistributedAlgorithm.AbstractServer(NewFakeServer())
</code></pre>
<p>I tried that out but reach the same error too.</p></pre>jiimji: <pre><p>no I mean do this : </p>
<pre><code> fakeServers := []FakeServer{ /* */ }
abstractServers := []AbstractServer{}
for _,el := range fakeServers {
abstractServers = append(abstractServers,el)
}
</code></pre>
<p>or do that :</p>
<pre><code>abstractServers := []AbstractServer{FakeServer{id:1},FakeServer{id:2}}
</code></pre></pre>wefnpwfpawfema: <pre><p>Ah, so this is actually the first thing I tried.</p>
<p>I have a <code>package main</code> that does exactly that, but the compiler will complain</p>
<pre><code>* serverImpl.FakeServer does not implement SomeCleverDistributedAlgorithm.AbstractServer (missing SomeCleverDistributedAlgorithm.doSomething method)
</code></pre></pre>jiimji: <pre><p>Oh, it's because doSomething is private, make it public with a capital letter. DoSomething both in the interface and implementation.</p></pre>wefnpwfpawfema: <pre><p>That does it, big thanks :) , didn't realize the case matters in determining access right, haha</p></pre>
[Beginner]What is the idiomatic way in golang to code against another packages's interface?
polaris · · 393 次点击这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传