go-github
go-github copied to clipboard
commit api
i meet a error when i request get commit api; this is my code: c := github.NewClient(&http.Client{}).WithAuthToken("xxxxxxxxxxxxxx") _, resp, err := c.Repositories.GetCommit(context.TODO(), "Jerry-yz", "go-zero-example", "013bdd7b435e9bff6e13617fa85b47c06883890c", &github.ListOptions{}) if err != nil { panic(err) } defer resp.Body.Close() commit := new(github.RepositoryCommit) b, err := io.ReadAll(resp.Body) if err != nil { panic(err) } if err := json.Unmarshal(b, commit); err != nil { panic(err) } panic: file already closed so, i find this err in client.Do this func read this resp.body and defer resp.close(), so i meet error when read resp.body from GetCommit() func
Yes, our Do
function performs most of this for you.
Instead, please try:
commit, _, err := c.Repositories.GetCommit(ctx, "Jerry-yz", "go-zero-example", "013bdd7b435e9bff6e13617fa85b47c06883890c", &github.ListOptions{})
if err != nil {
log.Fatalf("GetCommit: %v", err)
}
log.Printf("commit=%#v", commit)
Yes, our
Do
function performs most of this for you.Instead, please try:
commit, _, err := c.Repositories.GetCommit(ctx, "Jerry-yz", "go-zero-example", "013bdd7b435e9bff6e13617fa85b47c06883890c", &github.ListOptions{}) if err != nil { log.Fatalf("GetCommit: %v", err) } log.Printf("commit=%#v", commit)
so, i think this func should not return resp
If you take a look, all our methods return the resp
so that you can check other things such as the header, status code, etc.