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

前端之家收集整理的这篇文章主要介绍了[Go] 写文件和判断文件是否存在前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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 {
        存在)
    }
}

 

猜你在找的Go相关文章