mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 12:42:34 +02:00
20 lines
343 B
Go
20 lines
343 B
Go
package utils
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
func RespondJson(w http.ResponseWriter, data any, code ...int) error {
|
|
if len(code) > 0 {
|
|
w.WriteHeader(code[0])
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
j, err := json.MarshalIndent(data, "", " ")
|
|
if err != nil {
|
|
return err
|
|
} else {
|
|
w.Write(j)
|
|
}
|
|
return nil
|
|
}
|