https://github.com/barthr/newsapi
Go client for newsapi (https://newsapi.org/)
https://github.com/barthr/newsapi
go golang newsapi
Last synced: 5 months ago
JSON representation
Go client for newsapi (https://newsapi.org/)
- Host: GitHub
- URL: https://github.com/barthr/newsapi
- Owner: barthr
- License: mit
- Created: 2017-06-22T21:15:40.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2020-03-30T07:40:21.000Z (about 6 years ago)
- Last Synced: 2025-10-10T23:22:22.002Z (8 months ago)
- Topics: go, golang, newsapi
- Language: Go
- Homepage:
- Size: 37.1 KB
- Stars: 37
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: license
Awesome Lists containing this project
README
# NewsAPI Go Client
[](http://pkg.go.dev/github.com/barthr/newsapi) [](https://travis-ci.org/barthr/newsapi) [](https://codecov.io/gh/barthr/newsapi)
[](https://golangci.com/r/github.com/barthr/newsapi)
Go client for communicating with the newsapi's api.
## Getting Started
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
### Prerequisites
```
go get github.com/barthr/newsapi
```
Next, register for free at (https://newsapi.org/register), get yourself a free api key and keep it somewhere safe.
## Examples
### Retrieving all sources
```go
package main
import (
"fmt"
"net/http"
"context"
"github.com/barthr/newsapi"
)
func main() {
c := newsapi.NewClient("", newsapi.WithHTTPClient(http.DefaultClient))
sources, err := c.GetSources(context.Background(), nil)
if err != nil {
panic(err)
}
for _, s := range sources.Sources {
fmt.Println(s.Description)
}
}
```
### Retrieving all sources for a specific country (Great Britain in this case)
```go
package main
import (
"fmt"
"net/http"
"context"
"github.com/barthr/newsapi"
)
func main() {
c := newsapi.NewClient("", newsapi.WithHTTPClient(http.DefaultClient))
sources, err := c.GetSources(context.Background(), &newsapi.SourceParameters{
Country: "gb",
})
if err != nil {
panic(err)
}
for _, s := range sources.Sources {
fmt.Println(s.Name)
}
}
```
### Retrieving top headlines
```go
package main
import (
"fmt"
"net/http"
"context"
"github.com/barthr/newsapi"
)
func main() {
c := newsapi.NewClient("", newsapi.WithHTTPClient(http.DefaultClient))
articles, err := c.GetTopHeadlines(context.Background(), &newsapi.TopHeadlineParameters{
Sources: []string{ "cnn", "time" },
})
if err != nil {
panic(err)
}
for _, s := range articles.Articles {
fmt.Printf("%+v\n\n", s)
}
}
```
### Retrieving all articles
```go
package main
import (
"fmt"
"net/http"
"context"
"github.com/barthr/newsapi"
)
func main() {
c := newsapi.NewClient("", newsapi.WithHTTPClient(http.DefaultClient))
articles, err := c.GetEverything(context.Background(), &newsapi.EverythingParameters{
Sources: []string{ "cnn", "time" },
})
if err != nil {
panic(err)
}
for _, s := range articles.Articles {
fmt.Printf("%+v\n\n", s)
}
}
```
## License
This project is licensed under the MIT License
## Acknowledgments
* Inspired by github golang client