GolangNote

Golang笔记

GoLang 检测文件/文件夹是否存在并自动创建

Permalink

GoLang 检测文件/文件夹是否存在,若不存在则自动创建,常用到的一个功能代码。

Go: 自动创建文件/文件夹
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main

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

func main() {
    fmt.Println("Hello, 世界")
    path := "/tmp/to/create3"

    // check
    if _, err := os.Stat(path); err == nil {
        fmt.Println("path exists 1", path)
    } else {
        fmt.Println("path not exists ", path)
        err := os.MkdirAll(path, 0711)

        if err != nil {
            log.Println("Error creating directory")
            log.Println(err)
            return
        }
    }

    // check again
    if _, err := os.Stat(path); err == nil {
        fmt.Println("path exists 2", path)
    }
}

本文网址: https://golangnote.com/topic/92.html 转摘请注明来源

Related articles

Golang 把cookie 字符串解析为cookie 结构

在做爬虫时有时候会遇到需要带已登录的 cookie 请求,这个时候最简单的方法是在浏览器登录后,在开发者面板找到cookie 字符串,然后拷贝粘贴。这就面临一个问题需要把cookie 字符串解析成Go 语言 cookie 结构体。...

Golang 生成防识别的图片验证码

验证码 captcha 是对抗密码强力破解、垃圾信息的有效方式,一般用于用户注册、登录,当检测到频繁发帖时也会启用验证码。下面介绍用golang 生成防机器识别的图片验证码。...

Write a Comment to "GoLang 检测文件/文件夹是否存在并自动创建"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 0ms