Skip to content
This repository was archived by the owner on May 11, 2025. It is now read-only.

Commit 4ead61f

Browse files
committed
fix pointer error
1 parent 257212e commit 4ead61f

2 files changed

Lines changed: 18 additions & 15 deletions

File tree

api.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,22 @@ package main
22

33
import "fmt"
44

5-
func GetUser(username string) (res []interface{}, err error) {
6-
return Get(fmt.Sprintf("users/%s", username))
5+
func GetUser(username string) (data map[string]interface{}, err error) {
6+
err = Get(fmt.Sprintf("users/%s", username), &data)
7+
return data, err
78
}
89

9-
func GetRepos(username string) (res []interface{}, err error) {
10-
return Get(fmt.Sprintf("users/%s/repos", username))
10+
func GetRepos(username string) (data []interface{}, err error) {
11+
err = Get(fmt.Sprintf("users/%s/repos", username), &data)
12+
return data, err
1113
}
1214

13-
func GetRepo(username string, repo string) (res []interface{}, err error) {
14-
return Get(fmt.Sprintf("repos/%s/%s", username, repo))
15+
func GetRepo(username string, repo string) (data map[string]interface{}, err error) {
16+
err = Get(fmt.Sprintf("repos/%s/%s", username, repo), &data)
17+
return data, err
1518
}
1619

17-
func GetLanguages(username string, repo string) (res []interface{}, err error) {
18-
return Get(fmt.Sprintf("repos/%s/%s/languages", username, repo))
20+
func GetLanguages(username string, repo string) (data map[string]interface{}, err error) {
21+
err = Get(fmt.Sprintf("repos/%s/%s/languages", username, repo), &data)
22+
return data, err
1923
}

utils.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ func ScaleConvert(n float64, useSmallScale bool) string {
4747
return fmt.Sprintf("%.1f%s", n, scaleUnits[idx])
4848
}
4949

50-
func Get(uri string) (res []interface{}, err error) {
50+
func Get(uri string, ptr interface{}) (err error) {
5151
req, err := http.NewRequest("GET", "https://api.github.com/"+uri, nil)
5252
if err != nil {
53-
return nil, err
53+
return err
5454
}
5555

5656
req.Header.Set("Accept", "application/json")
@@ -59,7 +59,7 @@ func Get(uri string) (res []interface{}, err error) {
5959
client := &http.Client{}
6060
resp, err := client.Do(req)
6161
if err != nil {
62-
return nil, err
62+
return err
6363
}
6464

6565
defer func(Body io.ReadCloser) {
@@ -69,10 +69,9 @@ func Get(uri string) (res []interface{}, err error) {
6969
}
7070
}(resp.Body)
7171

72-
var data []interface{}
73-
err = json.NewDecoder(resp.Body).Decode(&data)
72+
err = json.NewDecoder(resp.Body).Decode(ptr)
7473
if err != nil {
75-
return nil, err
74+
return err
7675
}
77-
return data, nil
76+
return nil
7877
}

0 commit comments

Comments
 (0)