This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
modikai-dtool/utils/output_utils.go
2023-06-26 15:58:04 +08:00

25 lines
420 B
Go

package utils
import (
"encoding/json"
"fmt"
"io/ioutil"
)
func OutputJSON(data interface{}, filename string) error {
jsonstr, err := json.MarshalIndent(data, "", " ")
if err != nil {
fmt.Println("JSON encoding error:", err)
return err
}
if filename == "-" {
fmt.Println(string(jsonstr))
} else {
err := ioutil.WriteFile(filename, jsonstr, 0666)
if err != nil {
return err
}
}
return nil
}