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

相关推荐
jarreyer1 小时前
【爬虫】爬虫记录1
爬虫
亿牛云爬虫专家1 小时前
解决 Python 爬虫代理 407 错误:基于 urllib3 更新与爬虫代理的实战指南-2
爬虫·python·爬虫代理·authentication·urllib3·407·base64 编码
泰迪智能科技012 小时前
图书教材推荐|Python网络爬虫技术(第2版)(微课版)
开发语言·爬虫·python
NiKick2 小时前
Python 爬虫实战案例 - 获取社交平台事件热度并进行影响分析
开发语言·爬虫·python
userxxcc3 小时前
Waigo是用“Golang+Web”写的“视图窗口+稳定服务”的桌面端(Win、Mac、Ubuntu)多功能程序基座。开箱即用但有一定上手门槛。
javascript·golang·桌面应用基座·wails3
geovindu3 小时前
go: Prototype Pattern
开发语言·设计模式·golang·原型模式
码农很忙3 小时前
从零到英雄:使用 Playwright 实现高效网页数据爬取与自动化测试
爬虫·python
ccice014 小时前
python爬虫——爬取全年天气数据并做可视化分析
开发语言·爬虫·python
codeejun4 小时前
每日一Go-55、分布式 ID 生成(雪花算法 / Segment / Redis / DB)
数据库·分布式·golang
Tomhex13 小时前
Go容易出错的地方总结
golang