6

As mentioned in GoDocs, os.Create() creates a file in specific path.

os.Create("fonts/foo/font.eot")

But when fonts or foo doesn't exists, it returns panic: open fonts/foo/font.eot: The system cannot find the path specified.
So i used os.MkdirAll() to create nested directory. But there are many other problems with this function.

path := "fonts/foo/font.eot"
// this line create a directory named (font.eot) !
os.MkdirAll(path, os.ModePerm)

Is there any better way to create a file in nested directories?

4
  • 3
    That's the proper way to do such things - on the system level those map to mkdir and open system calls. What "many other problems" are you referring to? Commented Jan 29, 2020 at 6:26
  • 4
    It is os.MkdirAll("fonts/foo", 0770); os.Create("fonts/foo/font.eot") Dead simple and totaly obvious. Commented Jan 29, 2020 at 6:29
  • 1
    @Volker: most programs should probably use mode 0777 rather than 0770 here. File creation generally should use mode 0666. These allow the user's umask to take away group and/or other write permissions if desired, or even more permissions (resulting in modes like 0750 or 0700 for a directory) if desired. (I've seen this advice to use 0770 elsewhere, but am not sure where it originates.) Commented Jan 29, 2020 at 8:17
  • 4
    @torek Neither the filepaths nor the modes are fixed and need adoption to your particular use case. There is no single correct value. Commented Jan 29, 2020 at 10:29

1 Answer 1

32

The standard way is something like this:

func create(p string) (*os.File, error) {
    if err := os.MkdirAll(filepath.Dir(p), 0770); err != nil {
        return nil, err
    }
    return os.Create(p)
}

A few notes:

  • os.Create does not panic as stated in the question.
  • Create a directory from the directory part of the file path, not the full path.
Sign up to request clarification or add additional context in comments.

1 Comment

Second note is important!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.