优化
This commit is contained in:
Binary file not shown.
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -36,6 +35,32 @@ func isValidIP(ip string) bool {
|
||||
return net.ParseIP(ip) != nil
|
||||
}
|
||||
|
||||
func SaveBytesToFile(data []byte, filename string, syncToDisk bool) error {
|
||||
if filename == "" {
|
||||
return fmt.Errorf("filename cannot be empty")
|
||||
}
|
||||
|
||||
file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open file %q: %w", filename, err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = file.Write(data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to write to file %q: %w", filename, err)
|
||||
}
|
||||
|
||||
if syncToDisk {
|
||||
err = file.Sync()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to sync data to disk for file %q: %w", filename, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
var (
|
||||
err error
|
||||
@@ -105,11 +130,11 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatalf("读取响应体时出错: %v", err)
|
||||
}
|
||||
// 使用 bytes.Buffer 拼接 []byte 和字符串
|
||||
var buffer bytes.Buffer
|
||||
buffer.Write(HTTP_BODY)
|
||||
buffer.WriteString("\n")
|
||||
SaveBytesToFile(buffer.Bytes(), "response.json")
|
||||
|
||||
// 保存响应体到文件(可选)
|
||||
if err := SaveBytesToFile(append(HTTP_BODY, '\n'), "response.json", true); err != nil {
|
||||
log.Printf("failed to save HTTP response to file: %v", err)
|
||||
}
|
||||
|
||||
// 解析 JSON 数据
|
||||
err = json.Unmarshal(HTTP_BODY, &HTPP_JSON)
|
||||
@@ -124,24 +149,3 @@ func main() {
|
||||
fmt.Printf("Error!\n")
|
||||
}
|
||||
}
|
||||
|
||||
func SaveBytesToFile(data []byte, filename string) error {
|
||||
file, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open file: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
_, err = file.Write(data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to write to file: %w", err)
|
||||
}
|
||||
|
||||
// 如果你想要在每次写入后立即同步数据到磁盘(可选)
|
||||
err = file.Sync()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to sync data to disk: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,3 +4,5 @@
|
||||
{"code":"Success","data":{"continent":"北美洲","country":"美国","zipcode":"","owner":"","isp":"Cloudflare, Inc.","adcode":"","prov":"","city":"","district":""},"ip":"1.1.1.4"}
|
||||
{"code":"Success","data":{"continent":"北美洲","country":"美国","zipcode":"","owner":"","isp":"Cloudflare, Inc.","adcode":"","prov":"","city":"","district":""},"ip":"1.1.1.5"}
|
||||
{"code":"Success","data":{"continent":"北美洲","country":"美国","zipcode":"","owner":"","isp":"Cloudflare, Inc.","adcode":"","prov":"","city":"","district":""},"ip":"1.1.1.6"}
|
||||
{"code":"Success","data":{"continent":"北美洲","country":"美国","zipcode":"","owner":"","isp":"Cloudflare, Inc.","adcode":"","prov":"","city":"","district":""},"ip":"1.1.1.7"}
|
||||
{"code":"Success","data":{"continent":"北美洲","country":"美国","zipcode":"","owner":"","isp":"Cloudflare, Inc.","adcode":"","prov":"","city":"","district":""},"ip":"1.1.1.8"}
|
||||
|
||||
Reference in New Issue
Block a user