Go client implementation for the NewsAPI.
go get github.com/jellydator/newsapi-go
Simply, we create a client using NewClient
function, by default only an API
key is required, however some other parameters can be set using variadic
option functions.
client := newsapi.NewClient("apiKey", newsapi.WithHTTPClient(&http.Client{
Timeout: 5 * time.Second,
}))
Everything
retrieves all articles based on provided parameters.
Full endpoint documentation can be viewed here.
articles, pageCount, err := client.Everything(context.Background(), newsapi.EverythingParams{
Query: "cryptocurrency",
})
if err != nil {
// handle error
}
// success
TopHeadlines
retrieves top headlines articles based on provided parameters.
Full endpoint documentation can be viewed here.
articles, pageCount, err := client.TopHeadlines(context.Background(), newsapi.TopHeadlinesParams{
Query: "cryptocurrency",
})
if err != nil {
// handle error
}
// success
Sources
retrieves available sources based on provided parameters.
Full endpoint documentation can be viewed here.
sources, err := client.Sources(context.Background(), newsapi.SourceParams{
Categories: []newsapi.Category{
newsapi.CategoryBusiness,
newsapi.CategoryScience,
},
})
if err != nil {
// handle error
}
// success