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

3
myip-go/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module myip
go 1.23.0

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)
}

View File

@@ -3,5 +3,10 @@
"script_name": "helloworld-go",
"script_length": 128,
"script_sha256": "b807f5d29dd2dbd4dc8c665616c836b1e1c0274d377672109a3e2397bb1139bf"
},
"myip-go": {
"script_name": "myip-go",
"script_length": 798,
"script_sha256": "2abb41d64288774c01c465508070bec5ba7350228ab0dc79a2fe679b5afb07b6"
}
}