代码改变世界

go 通过bufio 写文件

  糯米粥  阅读(84)  评论(0编辑  收藏  举报
bufio是先写道缓冲区,通过 Flush() 更新
复制代码
func writeFile(content, fileName string) {
    create, err := os.Create(fileName) // 如果文件已存在,会将文件清空。

    if err != nil {
        panic(err)
    }
    defer create.Close()
    writer := bufio.NewWriter(create)
    //writer.WriteString(content)
    fmt.Fprint(writer, content)
    writer.Flush()
}
复制代码

 

 

func main() {
    writeFile("测试91", "./all/files/test2.txt")

}

 

复制代码
func main1() {
    writeFile("测试9", "abc1.txt")

    http.HandleFunc("/list/", func(writer http.ResponseWriter, request *http.Request) {
        //request.URL.Path
        //writer.Write()

        //err := errors.New("dd")

        file, err := os.Open(request.URL.Path)
        if err != nil {
            http.Error(writer, err.Error(), http.StatusInternalServerError)
            //或者
            http.Error(writer, http.StatusText(http.StatusNotFound), http.StatusNotFound)
            //文件不存在
            os.IsNotExist(err)

            //没有权限
            os.IsPermission(err)

            return
        }
        defer file.Close()

        bytes, err := io.ReadAll(file)
        if err != nil {
            panic(err)
        }
        writer.Write(bytes)
    })

    http.ListenAndServe(":8888", nil)
}
复制代码

 

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
历史上的今天:
2015-02-01 unity3d-游戏实战突出重围,整合游戏
'
点击右上角即可分享
微信分享提示