dongqi3533 2015-03-25 10:07
浏览 27
已采纳

从go-github Gist类型中提取信息

I have started learning Go and I find it quite interesting so far. As an assignment for myself to get get better at the language, I decided to write a Gister in Go using go-github.

I have been able to get all my Gists using an access token and I am able to print as follows:

package main

import "fmt"
import "github.com/google/go-github/github"
import "code.google.com/p/goauth2/oauth"

func main() {
    t := &oauth.Transport{
        Token: &oauth.Token{AccessToken: "secretaccesstokenhere"},
    }

    client := github.NewClient(t.Client())

    gists, _, err := client.Gists.List("", nil)

    if err != nil {
        fmt.Println(err)
    } else {
        for _, g := range gists {
            fmt.Printf("%v

", g.Files)
        }
    }
}

And I get the following output:

map[TODO.md:github.GistFile{Size:166, Filename:"TODO.md", RawURL:"somerawurlhere"}]

map[fourcore.c:github.GistFile{Size:309, Filename:"fourcore.c", RawURL:"somerawurlhere"}]

map[coretest.cpp:github.GistFile{Size:160, Filename:"coretest.cpp", RawURL:"somerawurlhere"}]

What I would like to print is "ID / FILENAME". I understand that I need to extract the ID from Gist type and Filename from above map but I wasn't able to find a way to do that. How do I do that? Help would be greatly appreciated.

P.S: Here is the documentation describing Gist type.

  • 写回答

1条回答 默认 最新

  • duanji1043 2015-03-25 16:47
    关注

    You have Files map, where filename is stored in key variable of type GistFilename, and ID is in Gist type variable. So you have to have two range's - one for Gists, other for Files. Something like this:

        for _, g := range gists {
            for filename, _ := range g.Files {
                fmt.Printf("%v / %v
    ", *g.ID, filename)
            }
        }
    

    Full code:

    package main
    
    import (
        "code.google.com/p/goauth2/oauth"
        "fmt"
        "github.com/google/go-github/github"
    )
    
    func main() {
        t := &oauth.Transport{
            Token: &oauth.Token{AccessToken: "secretaccesstokenhere"},
        }
    
        client := github.NewClient(t.Client())
    
        gists, _, err := client.Gists.List("", nil)
    
        if err != nil {
            fmt.Println(err)
            return
        }
        for _, g := range gists {
            for filename, _ := range g.Files {
                fmt.Printf("%v / %v
    ", *g.ID, filename)
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧