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


Api token can be easily created on

Api http request

Source Code

<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>    ""</div>
<div>    ""</div>
<div>    ""</div>
<div>    ""</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(";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>// 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>//    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>func (r *News) Marshal() ([]byte, error) {</div>
<div>    return json.Marshal(r)</div>
<div>type News struct {</div>
<div>    TotalArticles int64     `json:"totalArticles"`</div>
<div>    Articles      []Article `json:"articles"`</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>type Source struct {</div>
<div>    Name string `json:"name"`</div>
<div>    URL  string `json:"url"`</div>
Download Code

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: