Golang determine if a file exists and recursively create a folder

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

First, determine whether a file or folder exists

golangDetermining whether a file or folder exists can be os.stat() judged by methods and os.IsExist() methods:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  func isexist(path string)(bool){
_, Err: = OS. Stat (PATH)
if err! = Nil{
if os. Isexist (err) {
return true
}
if os. Isnotexist (err) {
return false
}
FMT. PRINTLN (ERR)
return false
}
return true
}

Ii. Recursive creation of folders

Recursive folder to os.MkdirAll() method:

1
func Mkdirall  string, perm FileMode) Error

The first parameter is the path, and the second is the permission. If the folder does not exist, it is created, and there is no action.

Third, the test code

1
2
3
4
5
6
7
8
9
Ten
One
A
-
-
the
-
-
-
+
-
+
A
at
-
-
-
-
-
in
-
to
+
-
the
*
$
Package   main
Import (
"OS"
"FMT"
)
//Determine if a file or folder exists
func isexist(path string)(bool){
_, Err: = OS. Stat (PATH)
if err! = Nil{
if os. Isexist (err) {
return true
}
if os. Isnotexist (err) {
return false
}
FMT. PRINTLN (ERR)
return false
}
return true
}
func main(){
//Recursive creation of folders
ERR: = OS. Mkdirall ("./test/1/2", OS. Modeperm)
if err! = Nil{
FMT. PRINTLN (ERR)
return
}
dirs: = []string{"./test/1", "./test/2", "./test/1.txt"}
for _, V: = range dirs{
if isexist (v) {
FMT. Printf ("%s is exist!", v)
}Else{
FMT. Printf ("%s is not exist!", v)
}
}
}

Execute in Terminal:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
ma@ma:/data/code/go/src/file_exist$ Tree
.
└──file_exist.go
0 Directories, 1 file
ma@ma:/data/code/go/src/file_exist$ Go run file_exist.go # running program
./TEST/1 is exist!
./TEST/2 is not exist!
./test/1.txt is not exist!
ma@ma:/data/code/go/src/file_exist$ Tree
.
├──file_exist.go
└──test
└──1
└──2
3 Directories, 1 file
ma@ma:/data/code/go/src/file_exist$ Touch test/1.txt # Create 1.txt
ma@ma:/data/code/go/src/file_exist$ Go run file_exist.go
./TEST/1 is exist!
./TEST/2 is not exist!
./test/1.txt is exist! # 1.txt exists
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.