go-github icon indicating copy to clipboard operation
go-github copied to clipboard

commit api

Open Jerry-yz opened this issue 1 year ago • 3 comments

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

Jerry-yz avatar Dec 07 '23 08:12 Jerry-yz

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)

gmlewis avatar Dec 07 '23 15:12 gmlewis

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

Jerry-yz avatar Dec 08 '23 02:12 Jerry-yz

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.

gmlewis avatar Dec 08 '23 02:12 gmlewis