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

huangapple go评论114阅读模式
英文:

Extracting information from go-github Gist type

问题

我已经开始学习Go语言,到目前为止我发现它非常有趣。为了提高自己的语言水平,我决定使用go-github在Go中编写一个Gister。

我已经能够使用访问令牌获取所有的Gists,并且能够按如下方式打印出来:

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\n\n", g.Files)
        }
    }
}

我得到以下输出:

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"}]

我想要打印的是"ID / 文件名"。我知道我需要从Gist类型中提取ID,从上面的map中提取文件名,但是我找不到方法来实现。我该如何做到这一点?非常感谢您的帮助。

附注:这里是描述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\n\n", 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

得分: 1

你有一个名为Files的映射,其中文件名存储在类型为GistFilename的key变量中,而ID存储在类型为Gist的变量中。因此,你需要有两个范围循环-一个用于Gists,另一个用于Files。

类似于这样:

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

完整代码:

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\n", *g.ID, filename)
        }
    }
}
英文:

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\n", *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\n", *g.ID, filename)
		}
	}
}

huangapple
  • 本文由 发表于 2015年3月25日 18:07:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/29252684.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定