How to Make HTTP GET Request in Go: Simple Guide
In Go, you can make an HTTP GET request using the
http.Get function from the net/http package. It returns a response and an error; you read the response body to get the data from the server.Syntax
The basic syntax to make an HTTP GET request in Go is using http.Get(url). It returns a *http.Response and an error. You must close the response body after reading it to free resources.
- url: The web address you want to request.
- resp: The response object containing status, headers, and body.
- err: Error if the request failed.
go
resp, err := http.Get("https://example.com") if err != nil { // handle error } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { // handle error } // use body data
Example
This example shows how to make a GET request to https://api.github.com, read the response body, and print it as a string.
go
package main import ( "fmt" "io" "net/http" ) func main() { resp, err := http.Get("https://api.github.com") if err != nil { fmt.Println("Error making GET request:", err) return } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { fmt.Println("Error reading response body:", err) return } fmt.Println(string(body)) }
Output
{
"current_user_url": "https://api.github.com/user",
"current_user_authorizations_html_url": "https://github.com/settings/connections/applications{/client_id}",
"authorizations_url": "https://api.github.com/authorizations",
"code_search_url": "https://api.github.com/search/code?q={query}{&page,per_page,sort,order}",
"commit_search_url": "https://api.github.com/search/commits?q={query}{&page,per_page,sort,order}",
"emails_url": "https://api.github.com/user/emails",
"emojis_url": "https://api.github.com/emojis",
"events_url": "https://api.github.com/events",
"feeds_url": "https://api.github.com/feeds",
"followers_url": "https://api.github.com/user/followers",
"following_url": "https://api.github.com/user/following{/target}",
"gists_url": "https://api.github.com/gists{/gist_id}",
"hub_url": "https://api.github.com/hub",
"issue_search_url": "https://api.github.com/search/issues?q={query}{&page,per_page,sort,order}",
"issues_url": "https://api.github.com/issues",
"keys_url": "https://api.github.com/user/keys",
"label_search_url": "https://api.github.com/search/labels?q={query}&repository_id={repository_id}{&page,per_page}",
"notifications_url": "https://api.github.com/notifications",
"organization_url": "https://api.github.com/orgs/{org}",
"organization_repositories_url": "https://api.github.com/orgs/{org}/repos{?type,page,per_page,sort}",
"organization_teams_url": "https://api.github.com/orgs/{org}/teams",
"public_gists_url": "https://api.github.com/gists/public",
"rate_limit_url": "https://api.github.com/rate_limit",
"repository_url": "https://api.github.com/repos/{owner}/{repo}",
"repository_search_url": "https://api.github.com/search/repositories?q={query}{&page,per_page,sort,order}",
"current_user_repositories_url": "https://api.github.com/user/repos{?type,page,per_page,sort}",
"starred_url": "https://api.github.com/user/starred{/owner}{/repo}",
"starred_gists_url": "https://api.github.com/gists/starred",
"user_url": "https://api.github.com/users/{user}",
"user_organizations_url": "https://api.github.com/user/orgs",
"user_repositories_url": "https://api.github.com/users/{user}/repos{?type,page,per_page,sort}",
"user_search_url": "https://api.github.com/search/users?q={query}{&page,per_page,sort,order}"
}
Common Pitfalls
Common mistakes when making HTTP GET requests in Go include:
- Not checking the error returned by
http.Get, which can cause your program to panic or behave unexpectedly. - Forgetting to close the response body with
defer resp.Body.Close(), which can lead to resource leaks. - Not reading the response body fully or incorrectly handling it.
go
package main import ( "fmt" "io" "net/http" ) func main() { // Wrong: Not checking error and not closing body resp, _ := http.Get("https://api.github.com") body, _ := io.ReadAll(resp.Body) resp.Body.Close() fmt.Println(string(body)) // Right: Check error and close body resp2, err := http.Get("https://api.github.com") if err != nil { fmt.Println("Error:", err) return } defer resp2.Body.Close() body2, err := io.ReadAll(resp2.Body) if err != nil { fmt.Println("Error reading body:", err) return } fmt.Println(string(body2)) }
Quick Reference
Remember these tips when making HTTP GET requests in Go:
- Use
http.Get(url)to send the request. - Always check the error returned.
- Use
defer resp.Body.Close()to close the response body. - Read the response body with
io.ReadAll(resp.Body). - Handle errors at every step to avoid crashes.
Key Takeaways
Use http.Get to make a GET request and always check for errors.
Always close the response body with defer resp.Body.Close() to avoid resource leaks.
Read the response body using io.ReadAll to get the data from the server.
Handle errors at every step to keep your program stable.
Remember to import net/http and io packages for HTTP requests and reading responses.