乐趣区

关于go:Go-快速入门指南-创建-删除目录

概述

调用 os 包即可。

例子

目录创立

package main

import (
    "fmt"
    "os"
)

func main() {err := os.Mkdir("/tmp/test_go_main_dir", 0755) // 创立 1 级目录
    if err != nil {panic(err)
    } else {fmt.Println("/tmp/test_go_main_dir has been created")
    }

    err = os.MkdirAll("/tmp/test_go_main_dir/1/2/3", 0755) // 创立多级目录
    if err != nil {panic(err)
    } else {fmt.Println("/tmp/test_go_main_dir/1/2/3 has been created")
    }
}

// $ go run main.go
// 输入如下
/**
  /tmp/test_go_main_dir has been created
  /tmp/test_go_main_dir/1/2/3 has been created
*/

目录删除

package main

import (
    "fmt"
    "os"
)

func main() {err := os.RemoveAll("/tmp/test_go_main_dir")
    if err != nil {panic(err)
    } else {fmt.Println("/tmp/test_go_main_dir has been deleted")
    }
}

// $ go run main.go
// 输入如下
/**
  /tmp/test_go_main_dir/1/2/3 has been deleted
*/

分割我

退出移动版