Go client for communicating with the newsapi's api.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.
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.
package main
import (
"fmt"
"net/http"
"context"
"github.com/barthr/newsapi"
)
func main() {
c := newsapi.NewClient("<API KEY>", 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)
}
}
package main
import (
"fmt"
"net/http"
"context"
"github.com/barthr/newsapi"
)
func main() {
c := newsapi.NewClient("<API KEY>", 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)
}
}
package main
import (
"fmt"
"net/http"
"context"
"github.com/barthr/newsapi"
)
func main() {
c := newsapi.NewClient("<API KEY>", 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)
}
}
package main
import (
"fmt"
"net/http"
"context"
"github.com/barthr/newsapi"
)
func main() {
c := newsapi.NewClient("<API KEY>", 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)
}
}
This project is licensed under the MIT License
- Inspired by github golang client