Avatar ·

How to create a folder in Go if it doesn't exist (mkdir if not exists)

Hello everyone! I just started learning Go and ran into this problem. I need to create a directory in my program for saving logs, but only if it doesn't exist yet. If the folder already exists — do nothing, just continue working. The problem is that the standard os.Mkdir() function returns an error if the directory already exists. I tried checking with os.Stat() first and then creating, but it looks like a workaround. In other languages (e.g., Python) there is os.makedirs(exist_ok=True), and in PHP — mkdir($path, 0777, true). Is there something similar in Go? Here's my current code, which seems wrong to me: ```go package main import ( "fmt" "os" ) func main() { path := "./logs" if _, err := os.Stat(path); os.IsNotExist(err) { err := os. ```