JIYIK CN >

Current Location:Home > Learning > PROGRAM > Go >

Create a new directory in Go

Author:JIYIK Last Updated:2025/04/15 Views:

Go has many ways to create directories, one of which is os.Mkdir. We can assign specified names and permission bits to the newly formed directory.

os.MkdirAllUseful for recursively creating multiple directories, including any missing parent directories.


Permission modes for os.Mkdir and os.MkdirAll

symbol Octal Integer functions
RX 7 Read, write and execute
rw- 6 Read, write
rx 5 Read, and execute
r-- 4 Read
-wx 3 Write and execute
-w- 2 Write
--x 1 Execute
--- 0 No permissions

User Access

Permissions Octal Fields
rwx------ 0700 User
---rwx--- 0070 Group
------rwx 0007 Other

General permissions

  • 0664: Only the owner can read and write the file. Other users can only read the file.
  • 0600: Only the owner can read and write the file; others cannot access it.
  • 0777: All users have full access to the file.
  • 0755: Only the owner has full access rights, other users can only view and execute the file.
  • 0666: Read and write permissions for all file users.
  • 0750: The file owner can read, write, and execute, but the user can only read and execute.

Using os.Mkdir() Method in Go

package main

import (
    "log"
    "os"
    "fmt"
)

func main() {

    error := os.Mkdir("HomeDir", 0750)

    if error != nil && !os.IsExist(error) {

        log.Fatal(error)

    }

    fmt.Print("The directory has been created successfully.")

}

Output:

The directory has been created successfully.

The above code allows us to create a new directory. If there is an error while creating the new directory, it will be of type PathError.

os.Mkdir function creates only one directory. We can create subdirectories using this function.


Using os.MkdirAll() Method in Go

package main

import (
    "log"

    "os"

    "fmt"
)

func main() {
    DirPath := "Desktop/Documents/MyDirectory"

    err := os.MkdirAll(DirPath, os.ModePerm)

    if err != nil {

        log.Println(err)

    }

    fmt.Print("The directory has been created successfully.")
}

Output:

The directory has been created successfully.

The ability to create multiple directories with a single command is helpful. Missing parent directories are automatically made, avoiding the redundancy of writing new directory code.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Install GoLang using Brew

Publish Date:2025/04/15 Views:82 Category:Go

This article describes how to install GoLang using Brew on Linux or macOS. Install GoLang using Brew brew installs missing packages in Linux and macOS. It makes it easy to install GoLang on Linux or macOS. Follow the steps below to install

GoLang RWMutex Detailed Introduction

Publish Date:2025/04/15 Views:116 Category:Go

This article introduces how to use rwmutex in Go language. Go language RWMutex mutex is the abbreviation of mutual exclusion, which is used to keep track of which thread has accessed a variable at any time. Mutex is a data structure provide

Enabling CORS in GoLang

Publish Date:2025/04/15 Views:102 Category:Go

This article describes how to enable and use CORS in GoLang. Go language CORS Cross-origin resource sharing (CORS) is a process based on HTTP headers that defines the origins from which browsers are allowed to load and use resources. CORS i

Function pointers in Go

Publish Date:2025/04/15 Views:83 Category:Go

A pointer in Go is a special-purpose variable that stores the memory address of other variables and the point where the memory is located. It can also access the value stored in that memory location. Pointer declaration in Go var pointer_na

Duration in Go

Publish Date:2025/04/15 Views:89 Category:Go

Go duration conversion can be done in a variety of ways. The time library and the time.duration method are often used to calculate and display time. Note that duration refers to the time elapsed between two defined time objects, as an int64

Getting a string representation of a structure in Go

Publish Date:2025/04/15 Views:63 Category:Go

Go allows us to serialize data from structures using a variety of simple standard methods. Converting a structure to a string using String method in Go The GoLang package String helps implement simple functions to manipulate and edit UTF-8

Convert JSON to struct in Go

Publish Date:2025/04/15 Views:126 Category:Go

This article describes how to convert JSON to struct in GoLang . Convert JSON to Struct using Unmarshal method in Go The encoding/json package of the Go language provides a function Unmarshal to convert JSON data into byte format. This func

Golang 中的零值 Nil

Publish Date:2023/04/27 Views:185 Category:Go

本篇文章介绍 nil 在 Golang 中的含义,nil 是 Go 编程语言中的零值,是众所周知且重要的预定义标识符。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial