What is my IP Country City – Fyne GUI Golang tutorial 19

 


package main

// import Fyne
import (
"encoding/json"
"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() {
// New app
a := app.New()
// New Title and Window
w := a.NewWindow("What is my IP?")
// Resize
w.Resize(fyne.NewSize(400, 400))
// Let Creat UI First
//Title
labelTitle := widget.NewLabel("What is my IP?")
labelIP := widget.NewLabel("Your IP is ...")
label_Value := widget.NewLabel("...")
label_City := widget.NewLabel("...")
label_Country := widget.NewLabel("...")
btn := widget.NewButton("Run", func() {
// Logic
label_Value.Text = myIP()
label_Value.Refresh()
label_City.Text = myCity()
label_City.Refresh()
label_Country.Text = myContry()
label_Country.Refresh()
})

w.SetContent(
container.NewVBox(
labelTitle,
labelIP,
label_Value,
label_City,
label_Country,
btn,
),
)
// Show and Run
w.ShowAndRun()
}

func myIP() string {
req, err := http.Get("http://ip-api.com/json/")
if err != nil {
return err.Error()
}

defer req.Body.Close()
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return err.Error()
}
var ip IP
json.Unmarshal(body, &ip)

return ip.Query
}

func myCity() string {
req, err := http.Get("http://ip-api.com/json/")
if err != nil {
return err.Error()
}

defer req.Body.Close()
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return err.Error()
}
var ip IP
json.Unmarshal(body, &ip)

return ip.City
}

func myContry() string {
req, err := http.Get("http://ip-api.com/json/")
if err != nil {
return err.Error()
}

defer req.Body.Close()
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return err.Error()
}
var ip IP
json.Unmarshal(body, &ip)

return ip.Country
}

type IP struct {
Query string
Country string
City string
}

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: