Create directory while creating file

I have to post a file via an http post request and save it on a folder called public/file, here is my code :

file, handler, err := r.FormFile("file")
defer file.Close()
if err != nil {

f, err := os.OpenFile("./public/file/"+handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
defer f.Close()
io.Copy(f, file)

The code works if the directory is already created but I want it to create the directory if it isn’t exist…
Do you have an idea about how to do this ?

Google said os.MkdirAll is a thing.

1 Like

Thx I didn’t found it :wink:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.