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

相关推荐
科技苑4 小时前
简单 Python 爬虫程序设计
爬虫
GO兔9 小时前
开篇:GORM入门——Go语言的ORM王者
开发语言·后端·golang·go
q5673152312 小时前
R语言初学者爬虫简单模板
开发语言·爬虫·r语言·iphone
代码讲故事16 小时前
多种方法实现golang中实现对http的响应内容生成图片
开发语言·chrome·http·golang·图片·快照·截图
weixin_4373982117 小时前
转Go学习笔记
linux·服务器·开发语言·后端·架构·golang
Code季风20 小时前
深入比较 Gin 与 Beego:Go Web 框架的两大选择
开发语言·golang·go·gin·beego
Code季风20 小时前
Gin 中间件详解与实践
学习·中间件·golang·go·gin
九班长20 小时前
Golang服务端处理Unity 3D游戏地图与碰撞的详细实现
3d·unity·golang
泡泡以安1 天前
安卓高版本HTTPS抓包:终极解决方案
爬虫·https·安卓逆向·安卓抓包
q567315231 天前
Java Selenium反爬虫技术方案
java·爬虫·selenium