码迷,mamicode.com
首页 > 其他好文 > 详细

[Go] 写文件和判断文件是否存在

时间:2019-11-22 00:59:14      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:pac   class   style   错误   写文件   nbsp   open   pack   file   

OpenFile得到一个File,然后调用它的Write,参数是字节切片
Stat看看返回错误没有

package main

import (
    "fmt"
    "os"
)

func main() {
    file := "1.txt"
    f, _ := os.OpenFile(file, os.O_RDWR|os.O_CREATE, 0766)
    f.Write([]byte("你好"))
    f.Close()

    //判断文件是否存在
    _, err := os.Stat(file)
    if err != nil && os.IsNotExist(err) {
        //存在
        fmt.Println("不存在")
    } else {
        fmt.Println("存在")
    }
}

 

[Go] 写文件和判断文件是否存在

标签:pac   class   style   错误   写文件   nbsp   open   pack   file   

原文地址:https://www.cnblogs.com/taoshihan/p/11909110.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!