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

25 lines
420 B
Go
Raw Normal View History

2023-06-16 18:55:18 +08:00
package utils
import (
"encoding/json"
"fmt"
2023-06-26 15:58:04 +08:00
"io/ioutil"
2023-06-16 18:55:18 +08:00
)
2023-06-26 15:58:04 +08:00
func OutputJSON(data interface{}, filename string) error {
2023-06-16 18:55:18 +08:00
jsonstr, err := json.MarshalIndent(data, "", " ")
if err != nil {
fmt.Println("JSON encoding error:", err)
2023-06-26 15:58:04 +08:00
return err
2023-06-16 18:55:18 +08:00
}
2023-06-26 15:58:04 +08:00
if filename == "-" {
fmt.Println(string(jsonstr))
} else {
err := ioutil.WriteFile(filename, jsonstr, 0666)
if err != nil {
return err
}
}
return nil
2023-06-16 18:55:18 +08:00
}