go(utils): add helper function to get config file handler

pull/7/head
oscar 2022-09-17 21:00:56 +12:00
parent 3f10f63c28
commit 3bb03bad87
1 changed files with 24 additions and 0 deletions

24
go/utils/config.go 100644
View File

@ -0,0 +1,24 @@
package utils
import (
"errors"
"fmt"
"os"
)
func GetConfigFile(name string) (*os.File, error) {
_, err := os.Stat(name)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
//create file
file, err := os.Create(name)
if err != nil {
return nil, fmt.Errorf("fail to create config file: %w", err)
}
return file, err
} else {
return nil, err
}
}
return os.OpenFile(name, os.O_RDWR, 0644)
}