Closed
Description
Hi,
golang noob here.
Instead of using REST calls I thought I'd use this package here to interact with GitHub.
For example when using Users.ListFollowers()
I get all the information back of all the followers.
For a subsequent step I only need the Login
property (the GitHub user name).
However, I can't figure out how to work with the output of above call, the type being gitub.User
.
How do I parse this output?
Thanks all!
Activity
gmlewis commentedon May 26, 2019
Hi @davidobrien1985, and welcome to Go!
I predict that if you give it a chance, Go will become one of your favorite (dare I say "go-to"?) programming languages!
Pro-tip 1: If you want to get really good at Go really fast, dig into solving problems on a website like https://codingame.com or http://adventofcode.com.
Pro-tip 2: I used to be a snob about IDEs and how they were only for the weak, but VSCode has truly world-class support for Go and I highly recommend using it.
Now, to answer your question, the auto-generated Godocs are a great place to start. Here's the call you are asking about:
https://godoc.org/github.com/google/go-github/github#UsersService.ListFollowers
You'll see that it returns a slice of
[]*User
and if you click on that, you see theUser
struct:https://godoc.org/github.com/google/go-github/github#User
Due to the nature of how JSON is handled in Go, we typically use pointers to fields so that we can use
omitempty
and the fields will benil
if not supplied. As a result, we added "accessors" to make accessing the fields much easier... which you will see below that struct.So if you only needed the
Login
property of each user (and optionally wanted to print their name), you could do something like this...Now actually, this is a simple but not complete example... GitHub has pagination... so if this user has a large number of followers, GitHub breaks up the response into many request/response pairs. I'll respond again later with a more complete example, but hopefully this will get you up-and-running quickly.
Have fun with Go, and feel free to ask questions any time.
gmlewis commentedon May 26, 2019
As promised, here is a complete example to list all the followers of a particular GitHub user:
Note that GitHub has API query rate limits... which you will quickly become an expert at if you hit their API too hard. I believe the limit is 5000 queries per hour if your program uses an authenticated user, otherwise the rate limit is something like 60 per hour. The program above uses unauthenticated requests.
I hope that helps. Please let me know if you have any questions.
(Also note that this example is not optimal... it makes one extra API call when it could take a look at the
response
header to see if there are actually more users before making the last API call... but for simplicity I make the extra API call above. Let me know if you really want to see an optimized version.)gmlewis commentedon May 26, 2019
Here's a better example that shouldn't call the API one extra time.
Note that I've seen some weirdness when playing with the
PerPage
value. 1000 seems to be the maximum value, but setting it to smaller numbers then increasing thePage
number will sometimes get you the same results, so be warned you might need to sanitize the results or experiment if you don't use 1000 forPerPage
.gmlewis commentedon May 26, 2019
I'm going to go ahead and close this issue. Feel free to reopen if you have any questions or if I misunderstood your original question.