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

 

[sourcecode lang=”go” autolinks=”false” classname=”myclass” collapse=”false” firstline=”1″ gutter=”true” highlight=”1-3,6,9″ htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false” title=”Source Code main.go”]

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
}

[/sourcecode] [easy_media_download url=”http://blogvali.com/wp-content/uploads/fyne-golang-downloads/main19.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: