URL shortner – Fyne Golang GUI tutorial 73

URL shortner – Fyne Golang GUI tutorial 73

package main
import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/container"
    "fyne.io/fyne/v2/widget"
)
func main() {
    a := app.New()
    w := a.NewWindow("URL Shortner...")
    w.Resize(fyne.NewSize(400, 400))
    title1 := widget.NewLabel("URL Shortner App")
    title1.Alignment = fyne.TextAlignCenter
    title1.TextStyle = fyne.TextStyle{Bold: true}
    // Get url for user
    long_url := widget.NewEntry()
    long_url.SetPlaceHolder("Enter long URL here...")
    short_url := widget.NewMultiLineEntry()
    short_url.MultiLine = true
    short_url.SetPlaceHolder("Short URL will displayed here...")
    btn := widget.NewButton("Shorten", func() {
        // url of api
        URL := fmt.Sprintf("https://api.1pt.co/addURL?long=%s", long_url.Text)
        // http response
        resp, _ := http.Get(URL)
        defer resp.Body.Close()
        // read the response body
        body, _ := ioutil.ReadAll(resp.Body)
        // parse json data
        uRLShortner, _ := UnmarshalURLShortner(body)
        // add the suffix received to main url
        t := "1pt.co/" + uRLShortner.Short
        fmt.Print(t)
        short_url.Text = "Here is your short url: \n" + t
        short_url.Refresh()
    })
    c := container.NewVBox(title1, long_url, btn, short_url)
    w.SetContent(c)
    w.ShowAndRun()
}
// This file was generated from JSON Schema using quicktype, do not modify it directly.
// To parse and unparse this JSON data, add this code to your project and do:
//
//    uRLShortner, err := UnmarshalURLShortner(bytes)
//    bytes, err = uRLShortner.Marshal()
func UnmarshalURLShortner(data []byte) (URLShortner, error) {
    var r URLShortner
    err := json.Unmarshal(data, &r)
    return r, err
}
func (r *URLShortner) Marshal() ([]byte, error) {
    return json.Marshal(r)
}
type URLShortner struct {
    Status  int64  `json:"status"`
    Message string `json:"message"`
    Short   string `json:"short"`
    Long    string `json:"long"`
}

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: