Skip to content

Instantly share code, notes, and snippets.

@Natata
Last active April 24, 2018 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Natata/fa4a3ca988a132554d2c4cea0e41dc5c to your computer and use it in GitHub Desktop.
Save Natata/fa4a3ca988a132554d2c4cea0e41dc5c to your computer and use it in GitHub Desktop.
function for create or open a file
func createOrOpenFile(dir, fn string) (*os.File, error) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.Mkdir(dir, 0700)
if err != nil {
log.Infof("mkdir %v fail", dir)
return nil, err
}
}
fallPath := filepath.Join(dir, fn)
return os.OpenFile(fallPath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment