News App DEMO Source Code – Fyne Golang Gui tutorial 70

News App DEMO Source Code – Fyne Golang Gui tutorial 70

In this tutorial we are going to consume news api from gnews.io

 

Api token can be easily created on gnews.io

Api http request


https://gnews.io/api/v4/top-headlines?token=API-Token

Source Code

<div>
<div>package main</div>
<div>import (</div>
<div>    "encoding/json"</div>
<div>    "fmt"</div>
<div>    "image/color"</div>
<div>    "io/ioutil"</div>
<div>    "net/http"</div>
<div>    "fyne.io/fyne/v2"</div>
<div>    "fyne.io/fyne/v2/app"</div>
<div>    "fyne.io/fyne/v2/canvas"</div>
<div>    "fyne.io/fyne/v2/container"</div>
<div>    "fyne.io/fyne/v2/widget"</div>
<div>)</div>
<div>var num_article int = 1</div>
<div>var news News</div>
<div>func main() {</div>
<div>    a := app.NewWithID("")</div>
<div>    w := a.NewWindow("News App")</div>
<div>    w.Resize(fyne.NewSize(300, 400))</div>
<div>    URL := fmt.Sprintf("https://gnews.io/api/v4/top-headlines?token=4ae5b4030f29f64bcc70e25417a83699&amp;lang=en&amp;max=100")</div>
<div>    //API</div>
<div>    res, _ := http.Get(URL)</div>
<div>    defer res.Body.Close()</div>
<div>    body, _ := ioutil.ReadAll(res.Body)</div>
<div>    news, _ = UnmarshalNews(body)</div>
<div>    fmt.Println(news)</div>
<div>    // num_article = int(news.TotalArticles)</div>
<div>    label2 := widget.NewLabel(fmt.Sprintf("No of articles:%d",</div>
<div>        news.TotalArticles))</div>
<div>    //show title</div>
<div>    label3 := widget.NewLabel(fmt.Sprintf("%s", news.Articles[1].Title))</div>
<div>    label3.TextStyle = fyne.TextStyle{Bold: true}</div>
<div>    label3.Wrapping = fyne.TextWrapBreak</div>
<div>    // show articles</div>
<div>    entry1 := widget.NewLabel(fmt.Sprintf("%s",</div>
<div>        news.Articles[1].Description))</div>
<div>    //entry1.MultiLine = true</div>
<div>    entry1.Wrapping = fyne.TextWrapBreak</div>
<div>    //entry1.Resize(fyne.NewSize(300, 400))</div>
<div>    //entry1.Resize(fyne.NewSize(300, 300))</div>
<div>    // label4 := widget.NewLabel(fmt.Sprintf("Article: %s", news.Articles[0].Description))</div>
<div>    btn := widget.NewButton("Next", func() {</div>
<div>        num_article += 1</div>
<div>        label3.Text = news.Articles[num_article].Title</div>
<div>        entry1.Text = news.Articles[num_article].Description</div>
<div>        label3.Refresh()</div>
<div>        entry1.Refresh()</div>
<div>    })</div>
<div>    label1 := canvas.NewText("News App", color.Black)</div>
<div>    label1.Alignment = fyne.TextAlignCenter</div>
<div>    label1.TextStyle = fyne.TextStyle{Bold: true}</div>
<div>    img := canvas.NewImageFromFile("news.png")</div>
<div>    // img.Resize(fyne.NewSize(150, 150))</div>
<div>    img.FillMode = canvas.ImageFillOriginal</div>
<div>    e := container.NewVBox(label1, label3, entry1, btn)</div>
<div>    e.Resize(fyne.NewSize(300, 300))</div>
<div>    c := container.NewBorder(img, label2, nil, nil, e)</div>
<div>    w.SetContent(c)</div>
<div>    w.ShowAndRun()</div>
<div>}</div>
<div>// This file was generated from JSON Schema using quicktype, do not modify it directly.</div>
<div>// To parse and unparse this JSON data, add this code to your project and do:</div>
<div>//</div>
<div>//    news, err := UnmarshalNews(bytes)</div>
<div>//    bytes, err = news.Marshal()</div>
<div>func UnmarshalNews(data []byte) (News, error) {</div>
<div>    var r News</div>
<div>    err := json.Unmarshal(data, &amp;r)</div>
<div>    return r, err</div>
<div>}</div>
<div>func (r *News) Marshal() ([]byte, error) {</div>
<div>    return json.Marshal(r)</div>
<div>}</div>
<div>type News struct {</div>
<div>    TotalArticles int64     `json:"totalArticles"`</div>
<div>    Articles      []Article `json:"articles"`</div>
<div>}</div>
<div>type Article struct {</div>
<div>    Title       string `json:"title"`</div>
<div>    Description string `json:"description"`</div>
<div>    Content     string `json:"content"`</div>
<div>    URL         string `json:"url"`</div>
<div>    Image       string `json:"image"`</div>
<div>    PublishedAt string `json:"publishedAt"`</div>
<div>    Source      Source `json:"source"`</div>
<div>}</div>
<div>type Source struct {</div>
<div>    Name string `json:"name"`</div>
<div>    URL  string `json:"url"`</div>
<div>}</div>
</div>
[easy_media_download url=”http://blogvali.com/wp-content/uploads/fyne-golang-downloads/main56.go” text=”Download Code” color=”red_darker”]

Fyne Golang GUI Course

Tony BB
 

TonyBB is a Coach , marketer, hypnotist and a founder of RSKVF Production who specializes in providing simple, affordable, and easy to use solutions for Life.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: