package model import ( "encoding/json" "log" "os" ) // SaveJSON to path func SaveJSON(input interface{}, outputFile string) { tmpFile := outputFile + ".tmp" f, err := os.OpenFile(tmpFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) if err != nil { log.Panic(err) } err = json.NewEncoder(f).Encode(input) if err != nil { log.Panic(err) } f.Close() if err := os.Rename(tmpFile, outputFile); err != nil { log.Panic(err) } }