day 16 part 1

This commit is contained in:
RussellChamp committed 2021-12-16 15:54:05 -05:00
1 parent 4737afa383
commit 0c277a8404
11 files changed
+463 -1

No files matched your search

+27
View File
@@ -0,0 +1,27 @@
package files
import (
"errors"
"fmt"
"os"
)
func CreateOrReplace(fileName string) (*os.File, error) {
var output *os.File
if _, err := os.Stat(fileName); errors.Is(err, os.ErrNotExist) {
output, err = os.Create(fileName)
if err != nil {
return nil, fmt.Errorf("Error creating file %s", fileName)
}
} else {
err = os.Truncate(fileName, 0)
if err != nil {
return nil, fmt.Errorf("Error truncating file %s", fileName)
}
output, err = os.Open(fileName)
if err != nil {
return nil, fmt.Errorf("Error opening file %s", fileName)
}
}
return output, nil
}