golang的文件操作

Go 复制代码
package   main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {

	file,err :=os.OpenFile("D:/Kugou/nihao.txt",os.O_RDWR |os.O_APPEND | os.O_CREATE,0777)
    if err != nil {
    	fmt.Println("打开失败了",err)
		return
	}
	defer file.Close()
    write := bufio.NewWriter(file)
	for i :=0 ;i <5 ; i++ {
		write.WriteString("你好 golang \n")
	}
  write.Flush()
}

os.OpenFile,OS包的方法打开文件,将程序的内容写入到缓存中,传给变量,来展示

Go 复制代码
package  main

import (
	"fmt"
	"io/ioutil"
)

func main() {

	file1Path :="D:/Kugou/nihao.txt"
	file2Path := "D:/Kugou/test.txt"
  content,err :=	ioutil.ReadFile(file1Path)
	if err != nil {
		fmt.Println("有问题")
		return
  }

  err = ioutil.WriteFile(file2Path,content,0777)
	if err != nil {
		fmt.Println("写失败了")
	}





}

ioutil.ReadFile 读取文件,写入到变量,传入到路径下

相关推荐
tang7778918 小时前
异步爬虫与代理IP池结合:用aiohttp提升10倍抓取效率
爬虫·网络爬虫·爬虫代理·代理ip·代理ip池
codeejun19 小时前
每日一Go-76(架构篇)|多集群部署 / 容灾 / Failover / Backup / 热迁移
开发语言·架构·golang
深蓝电商API21 小时前
行为模拟的艺术:如何让爬虫的鼠标轨迹像真人
爬虫
迷茫运维路21 小时前
golang_Viper配置管理器
后端·golang
嫂子的姐夫21 小时前
047-MD5:飞卢网
爬虫·python·js逆向·逆向
geovindu1 天前
go: Broadcast Pattern
开发语言·后端·设计模式·golang·广播模式
~|Bernard|1 天前
关于go语言中二维切片的append操作陷阱
开发语言·后端·golang
数据知道1 天前
从Playwright到自研:构建指纹浏览器的技术栈选型与路线图
爬虫·数据采集·指纹浏览器
嫂子的姐夫1 天前
050-wx小程序合肥住房
爬虫·python·小程序·逆向
yijianace1 天前
Python爬虫学习记录—— BooksToScrape分页爬取与图片下载
爬虫·python