项目作者: PaulRosset

项目描述 :
📟 Tiny utility Go client for HackerNews API.
高级语言: Go
项目地址: git://github.com/PaulRosset/go-hacknews.git
创建时间: 2017-08-10T20:44:02Z
项目社区:https://github.com/PaulRosset/go-hacknews

开源协议:MIT License

下载


go-hacknews

Open Source Love
Awesome
Travis
Go Report Card


MIT Licence

Tiny utility Go client for HackerNews API.

Official Hackernews API

Install

  1. go get github.com/PaulRosset/go-hacknews

Usage

Few examples are available inside the examples/ folder.

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/PaulRosset/go-hacknews"
  5. )
  6. func main() {
  7. // Init struct with the kind of story you want
  8. // (topstories/newstories/beststories/askstories/showstories/jobstories)
  9. // and the number of posts that you want to fetch.
  10. init := hacknews.Initializer{"topstories", 10}
  11. // Get the code of posts.
  12. // Return a slice of int with the entry id if everything is ok or return an error.
  13. codes, err := init.GetCodesStory()
  14. if err != nil {
  15. fmt.Println(err)
  16. return
  17. }
  18. // Get the posts thanks their id fetched above.
  19. // Return a slice of Post type with a readable format in go or return an err if fail.
  20. posts, err := init.GetPostStory(codes)
  21. if err != nil {
  22. fmt.Println(err)
  23. return
  24. }
  25. // Iterate over the slice to get what we want.
  26. // Here, get Title and Url of post. But we can access all fields from the official hackernews api
  27. // Note : Field are empty, if no data belong to them
  28. for _, post := range posts {
  29. fmt.Printf("Title : %v // Url : %v\n", post.Title ,post.Url)
  30. }
  31. }

Test

Tests are available :

  1. $ go test

License

MIT