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 读取文件,写入到变量,传入到路径下

相关推荐
电商API_180079052473 小时前
拍照找同款是怎么实现的?淘宝以图搜图接口 item_search_img 对接实战
大数据·爬虫·数据挖掘·数据分析·代采api
Patrick在香港4 小时前
Python 抓 0.91 GB 香港法例:Agent 的进度该写进磁盘,不是写进上下文
爬虫·python·api·claude·香港
Experience-摆渡5 小时前
开源RAG知识库WeKnora深度调研:让知识自己长成体系
爬虫·docker·开源·rag
孙启超1 天前
【AI开发之Rust】第 7 课:错误处理 —— panic、Result 与 `?`
人工智能·分布式·后端·爬虫·spring cloud·架构·rust
chenqianghqu1 天前
go语言SDK升级到1.27更新调试
golang
ttwuai1 天前
Go开源后台管理系统推荐:怎么先排除 Fork、镜像和同名项目?
golang
该逃避避2 天前
IP代理类型介绍:住宅IP、机房IP、移动IP等区别与选择
爬虫
科技苑2 天前
Python简单网络爬虫教程
爬虫·python
参.商.2 天前
【Day 53】76. 最小覆盖子串
leetcode·golang
szephyr2 天前
Python 爬虫合规与反爬实战:从 requests 到 Playwright
爬虫·python·requests·playwright·反爬