课程: Go Essentials: Concurrency, Connectivity, and High-Performance Apps
今天就学习课程吧!
今天就开通帐号,24,700 门业界名师课程任您挑!
Solution: Call the GitHub API - Go教程
课程: Go Essentials: Concurrency, Connectivity, and High-Performance Apps
Solution: Call the GitHub API
(upbeat music) - [Instructor] Let's check out my solution. First thing I'm doing is letting the json package know that the NumRepos fills in the User struct, comes from the public_repos in the json document. Next, I have the user info. First, I'm constructing the URL using Sprintf and url.PathEscape to make sure that I get a valid URF. Then I'm making an HTP request and checking for an error. If there is no error, I'm making sure that I close the response body. I'm also checking the response status code to make sure that I got a good status code. Otherwise, I return an error with the status, not the status code from the response. After that, I'm decoding the json. I'm creating a user pre-populated with the login, using a json decoder to decode. If there is an error, I'm returning this here. Finally, I'm returning a pointer to the user, and nil signifying no error. The main did not change. Let's run it. You see that I…