go-github question

xuanbao · · 493 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hi guys,</p> <p>This is my first project in Go, having experimented with it before. For this project I need to retrieve the licenses for GitHub repositories, so I&#39;m using <a href="https://godoc.org/github.com/google/go-github/github" rel="nofollow">go-github</a>.</p> <p>The <a href="https://godoc.org/github.com/google/go-github/github#Repository" rel="nofollow">Repository</a> type has a License field, but the documentation says <em>&#34;// Only provided when using RepositoriesService.Get while in preview&#34;</em>. Indeed, my application segfaults when I try to access a License&#39;s name field directly:</p> <pre><code>license := repo.License.Name </code></pre> <p>Now the documentation says this field is available while in preview, but I have no idea what this is. There is no reference to &#34;preview&#34; in that page, and a Google search doesn&#39;t turn up anything useful either. Does anyone here know what I am to do?</p> <hr/>**评论:**<br/><br/>gogroob: <pre><p>It means that particular API feature is in developer preview: <a href="https://developer.github.com/v3/licenses/#get-a-repositorys-license" rel="nofollow">https://developer.github.com/v3/licenses/#get-a-repositorys-license</a></p> <blockquote> <p>The Licenses API is currently available for developers to preview. During the preview period, the API may change without advance notice. Please see the blog post for full details.</p> <p>To access the API during the preview period, you must provide a custom media type in the Accept header:</p> <p><code>application/vnd.github.drax-preview+json</code></p> </blockquote> <p>A lot of the go-github struct fields are pointers because the response objects from the github API are dynamic. You should check if each field you intend to use is not nil before accessing it&#39;&#39;s value to avoid crashing your service. </p></pre>fallenunia: <pre><p>Thanks for your reply. I was actually aware of the GitHub preview mode but did not connect the dots. As a follow-up question, do you know how I can provide that custom media type when using go-github?</p> <p><strong>Edit:</strong> As far as I can see, the media type is set by go-github: <a href="https://github.com/google/go-github/blob/a77ccc9a588d217ea8c8b069620a3a070ed201c3/github/repos.go#L176" rel="nofollow">https://github.com/google/go-github/blob/a77ccc9a588d217ea8c8b069620a3a070ed201c3/github/repos.go#L176</a></p></pre>fallenunia: <pre><p>Ah, never mind. I got it working. The API indeed sets the media type by default; <code>Repository.License</code> is however still <code>nil</code> if GitHub&#39;s API cannot find a license for it. The following code works:</p> <pre><code>repos, _, err := client.Repositories.List(user, opt) if err != nil { log.Fatal(err) } for _, repo := range repos { name := repo.Name var license string if repo.License != nil { license = *repo.License.Name } else { license = &#34;N/A&#34; } fmt.Printf(&#34;Repository %q by %q has license: %q\n&#34;, *name, user, license) } </code></pre></pre>

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

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