You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
595 B
Go
35 lines
595 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
func main() {
|
|
result, err := getIP("https://whatismyipv6.buddy.wtf/json")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
fmt.Println(result.IP)
|
|
}
|
|
|
|
type whatIsMyIPResult struct {
|
|
IP string `json:ip`
|
|
Version string `json:version`
|
|
}
|
|
|
|
func getIP(url string) (*whatIsMyIPResult, error) {
|
|
result, err := http.Get(url)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer result.Body.Close()
|
|
ipResult := &whatIsMyIPResult{}
|
|
if err := json.NewDecoder(result.Body).Decode(ipResult); err != nil {
|
|
return nil, err
|
|
}
|
|
return ipResult, nil
|
|
}
|