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 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化