feat: add myip-go

This commit is contained in:
2024-12-29 16:33:18 +08:00
parent 30ff33e719
commit 550bdc0f3d
3 changed files with 47 additions and 0 deletions

39
myip-go/main.go Executable file
View File

@@ -0,0 +1,39 @@
/// 2>/dev/null ; gorun "$0" "$@" ; exit $?
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
)
const GetIpUrl = "https://hatter.ink/ip/ip.jsonp"
type IpResponse struct {
Status int16 `json:"status"`
Message string `json:"message"`
IP string `json:"ip"`
UserAgent string `json:"userAgent"`
}
func main() {
response, err := http.Get(GetIpUrl)
if err != nil {
fmt.Printf("[ERROR] Get IP failed: %s\n", err)
os.Exit(1)
}
body, err := io.ReadAll(response.Body)
if err != nil {
fmt.Printf("[ERROR] Get IP failed: %s\n", err)
os.Exit(2)
}
var ipReponse IpResponse
if err := json.Unmarshal(body, &ipReponse); err != nil {
fmt.Printf("[ERROR] Get IP failed: %s\n", err)
os.Exit(3)
}
fmt.Printf("[INFO] Your IP address: %s\n", ipReponse.IP)
}