This commit is contained in:
2025-12-31 16:06:58 +07:00
commit ce230479c7
21 changed files with 797 additions and 0 deletions

34
utils/utils.go Normal file
View File

@@ -0,0 +1,34 @@
package utils
import (
"encoding/json"
"fmt"
"net/http"
)
func GetĐataJSON[T any](url string) (*T, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http status %d", resp.StatusCode)
}
var data T
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}