1 package main 2 3 import ( 4 "fmt" 5 "os" 6 ) 7 8 //判断文件夹是否存在 9 func PathExists(path string) (bool, error){ 10 _, err := os.Stat(path) 11 if err == nil{ 12 return true, nil 13 } 14 if os.IsNotExist(err){ 15 return false, nil 16 } 17 return false, err 18 } 19 20 func main() { 21 _dir := "C:\CodeWorkspace\GOWorkspace\1" 22 exist, err := PathExists(_dir) 23 if err != nil{ 24 fmt.Printf("get dir error![%v] ",err) 25 return 26 } 27 if exist{ 28 fmt.Printf("has dir![%v] ", _dir) 29 }else{ 30 fmt.Printf("no dir![%v] ", _dir) 31 //创建文件夹 32 err := os.Mkdir(_dir, os.ModePerm) 33 if err != nil{ 34 fmt.Printf("mkdir falied[%v] ", err) 35 }else{ 36 fmt.Printf("mkdir success! ") 37 } 38 } 39 }
实验现象
no dir![C:CodeWorkspaceGOWorkspace1]
mkdir success!