Skip to content

Instantly share code, notes, and snippets.

@databus23
Created January 19, 2026 13:26
Show Gist options
  • Select an option

  • Save databus23/5992ba6604959de9e370f85595743ff5 to your computer and use it in GitHub Desktop.

Select an option

Save databus23/5992ba6604959de9e370f85595743ff5 to your computer and use it in GitHub Desktop.
interview solution
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"sort"
"strings"
)
type Repo struct {
Name string `json:"name"`
License struct {
Name string `json:"name"`
} `json:"license"`
}
func fetchRepos(url string) ([]Repo, error) {
res, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("Failed to fetch the repos, error: %w", err)
}
defer res.Body.Close()
nextURL := ""
if l := res.Header.Get("Link"); l != "" {
parts := strings.Split(l, ",")
for _, p := range parts {
if strings.Contains(p, "rel=\"next\"") {
// Extract the URL from the link header.
nextURL = strings.Trim(strings.Split(strings.Split(p, ";")[0], "<")[1], ">")
fmt.Printf("Next URL: %s\n", nextURL)
}
}
}
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("Failed to read the response body, error: %w", err)
}
var repos []Repo
if err := json.Unmarshal(body, &repos); err != nil {
return nil, fmt.Errorf("Failed to unmarshal the response body, error: %w", err)
}
if nextURL != "" {
nextRepos, err := fetchRepos(nextURL)
if err != nil {
return nil, fmt.Errorf("Failed to fetch the next repos, error: %w", err)
}
repos = append(repos, nextRepos...)
}
return repos, nil
}
func main() {
const url = "https://api.github.com/orgs/sapcc/repos"
repos, err := fetchRepos(url)
if err != nil {
log.Fatalf(err.Error())
}
sort.Slice(repos, func(i, j int) bool {
return repos[i].Name < repos[j].Name
})
for _, repo := range repos {
fmt.Printf("Repo: %s, License: %s\n", repo.Name, repo.License.Name)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment