Skip to content

Repository files navigation

NewsAPI Go Client

GoDocBuild StatuscodecovGolangci

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

package main
import (
"fmt""net/http""context""github.com/barthr/newsapi"
)
funcmain() {
c:=newsapi.NewClient("<API KEY>", newsapi.WithHTTPClient(http.DefaultClient))
sources, err:=c.GetSources(context.Background(), nil)
iferr!=nil {
panic(err)
}
for_, s:=rangesources.Sources {
fmt.Println(s.Description)
}
}

Retrieving all sources for a specific country (Great Britain in this case)

package main
import (
"fmt""net/http""context""github.com/barthr/newsapi"
)
funcmain() {
c:=newsapi.NewClient("<API KEY>", newsapi.WithHTTPClient(http.DefaultClient))
sources, err:=c.GetSources(context.Background(), &newsapi.SourceParameters{
Country: "gb",
})
iferr!=nil {
panic(err)
}
for_, s:=rangesources.Sources {
fmt.Println(s.Name)
}
}

Retrieving top headlines

package main
import (
"fmt""net/http""context""github.com/barthr/newsapi"
)
funcmain() {
c:=newsapi.NewClient("<API KEY>", newsapi.WithHTTPClient(http.DefaultClient))
articles, err:=c.GetTopHeadlines(context.Background(), &newsapi.TopHeadlineParameters{
Sources: []string{ "cnn", "time" },
})
iferr!=nil {
panic(err)
}
for_, s:=rangearticles.Articles {
fmt.Printf("%+v\n\n", s)
}
}

Retrieving all articles

package main
import (
"fmt""net/http""context""github.com/barthr/newsapi"
)
funcmain() {
c:=newsapi.NewClient("<API KEY>", newsapi.WithHTTPClient(http.DefaultClient))
articles, err:=c.GetEverything(context.Background(), &newsapi.EverythingParameters{
Sources: []string{ "cnn", "time" },
})
iferr!=nil {
panic(err)
}
for_, s:=rangearticles.Articles {
fmt.Printf("%+v\n\n", s)
}
}

License

This project is licensed under the MIT License

Acknowledgments

  • Inspired by github golang client

About

Go client for newsapi (https://newsapi.org/)

Topics

Resources

Stars

37 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages