Help needed (noob): type XX interface { ... } with no implementation?

xuanbao · · 502 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello.</p> <p>So I&#39;ve been looking at using <a href="https://github.com/src-d/go-git" rel="nofollow">go-git</a> and can&#39;t clone my repo, because I need authorisation as well. So, there&#39;s the function &#34;git.PlainClone&#34; which takes a <a href="https://godoc.org/gopkg.in/src-d/go-git.v4#CloneOptions" rel="nofollow">&#34;git.CloneOptions&#34;</a> type struct. This &#34;CloneOptions&#34; has the property &#34;Auth&#34; and it expects a <a href="https://godoc.org/gopkg.in/src-d/go-git.v4/plumbing/transport#AuthMethod" rel="nofollow">&#34;transport.AuthMethod&#34;</a>. I just can&#39;t seem to be able to figure out what that really means. How to I correctly pass anything here? The method &#34;Name()&#34; isn&#39;t even implemented. Should I do that? I&#39;m just lost... thanks.</p> <p>EDIT: Solution found. Leaving this here for posterity and maybe even for my forgetful future self. I was unable (just too lazy) to find the implementation of the interface. One example can be seen <a href="https://github.com/src-d/go-git/blob/master/plumbing/transport/http/common.go#L175" rel="nofollow">here</a>. So, just basically <code>bauth = NewBasicAuth(&#34;uname&#34;,&#34;secret&#34;)</code> and pass that by way of <code>Auth: http.bauth</code> (since http is a part of the <code>transport</code> package and implements <code>AuthMethod</code> interface thus conforming to <code>transport.AuthMethod</code>)</p> <hr/>**评论:**<br/><br/>Sythe2o0: <pre><p>You give it a struct with a <code>String() string</code> function (what&#39;s behind <code>fmt.Stringer</code>) and a <code>Name() string</code> function, and that struct implicitly satisfies the <code>AuthMethod</code> interface. </p></pre>blackcomb-pc: <pre><p>I&#39;m not sure I follow you. So, I have to create some <code>type myauth struct</code> for which I implement the <code>Stringer()</code> and <code>Name()</code> functions? And then I&#39;d create a variable of the type <code>myauth</code> and then I&#39;d pass that? I mean there&#39;s no <code>Name()</code> implementation anywhere. And what would I write anyway, like <code>return &#34;ssh&#34;</code>? Totally confused. I guess I don&#39;t understand what to pass here:</p> <pre><code> _, err := git.PlainClone(&#34;git/repo&#34;, false, &amp;git.CloneOptions{ URL: &#34;muh.git&#34;, // Auth: transport.AuthMethod, &lt;----- ??? RecurseSubmodules: git.DefaultSubmoduleRecursionDepth, Progress: os.Stdout, }) </code></pre></pre>Sythe2o0: <pre><p>OK I dug through files and can be a bit more helpful.</p> <p>Auth is finally used in places like <a href="https://github.com/src-d/go-git/blob/v4/plumbing/transport/git/common.go#L22" rel="nofollow">this</a>. Git does not support ANY auth, is what it is saying, so it wants you to not populate that field. </p> <p>HTTP on the other hand expects you to pass in its own private <code>AuthMethod</code> interface, like <a href="https://github.com/src-d/go-git/blob/v4/plumbing/transport/http/common.go#L158" rel="nofollow">BasicAuth</a></p></pre>blackcomb-pc: <pre><p>Thanks for the enlightening me! That is exactly what I needed to see. Will edit the post.</p></pre>LimEJET: <pre><p>The ssh package has <a href="https://godoc.org/golang.org/x/crypto/ssh#AuthMethod" rel="nofollow">a set of <code>AuthMethod</code>s that you can use</a>. Try passing one of those.</p></pre>kemitche: <pre><p>That&#39;s a distinct interface from the transport.AuthMethod in OP&#39;s question, unfortunately.</p></pre>LimEJET: <pre><p>That seems odd, since git works over ssh. You sure? I can&#39;t really check on my phone.</p></pre>kemitche: <pre><p>Yup, I&#39;m sure. OPs linked interface has exported methods, your linked interface only has unexported methods.</p></pre>LimEJET: <pre><p>But aren&#39;t they in the same package? Or is it a different <code>transport</code>?</p></pre>kemitche: <pre><p>Different. OP is looking at <code>github.com/src-d/go-git/blob/master/plumbing/transport.AuthMethod</code>, you&#39;re looking at <code>golang.org/x/crypto/ssh.AuthMethod</code></p></pre>blackcomb-pc: <pre><p>That&#39;s right. Still trying to get used to orient myself within the source file layout of a typical go project. Thanks for the input tho!</p></pre>blackcomb-pc: <pre><p>Thanks, man, but I figured it out. See Post edit.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

502 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传