diff --git a/util.go b/util.go index 4024a9d..bd601bb 100644 --- a/util.go +++ b/util.go @@ -161,11 +161,17 @@ func getPrefixForStruct(prefixes []string, fieldStruct *reflect.StructField) []s func marshalFile(target interface{}, file string, overWrite bool) error { var f *os.File var err error + + var b []byte + if b, err = marshal(target, filepath.Ext(file)); nil != err { + return err + } + if Exists(file) { if !overWrite { return fmt.Errorf("Config: File[%s] is exist", file) } - if f, err = os.Open(file); nil != err { + if f, err = os.OpenFile(file, os.O_WRONLY, os.ModeAppend); nil != err { return err } } else { @@ -173,11 +179,7 @@ func marshalFile(target interface{}, file string, overWrite bool) error { return err } } - - var b []byte - if b, err = marshal(target, filepath.Ext(file)); nil != err { - return err - } + defer f.Close() if _, err = f.Write(b); nil != err { return err