您的位置:首页 > 健康 > 美食 > 建工网校和建工社是一个吗_邢台头条新闻_申京效率值联盟第一_百度指数数据来源

建工网校和建工社是一个吗_邢台头条新闻_申京效率值联盟第一_百度指数数据来源

2025/5/21 4:39:39 来源:https://blog.csdn.net/shanshui521/article/details/147235260  浏览:    关键词:建工网校和建工社是一个吗_邢台头条新闻_申京效率值联盟第一_百度指数数据来源
建工网校和建工社是一个吗_邢台头条新闻_申京效率值联盟第一_百度指数数据来源

第八章 文件操作

文章目录

    • 第八章 文件操作
      • 1 文件读取
        • 1 将文件整个读取内存
        • 2 按字节读取文件

1 文件读取

1 将文件整个读取内存

类似于python的

with open(filename, mode='rt', encoding='utf-8') as f:res = f.read()

go中的书写方式:

  • 方式一:
package mainimport ("fmt""io/ioutil""log""os"
)func main() {// 打开文件,以只读的方式打开file, err := os.Open("C:\\Users\\YangYi\\Desktop\\动作.")if err != nil {//println(err)  // (0x2a0ba0,0xc000076330)log.Panic(err)  // 使用这种方式打印错误/*2022/03/17 08:49:31 open C:\Users\YangYi\Desktop\动作.: The system cannot find the file specified.panic: open C:\Users\YangYi\Desktop\动作.: The system cannot find the file specified*///panic(err)}defer file.Close()content, err := ioutil.ReadAll(file)  // 类似于python中的f.read()fmt.Println(string(content))
}
  • 方式二:
package mainimport ("fmt""io/ioutil""log"
)func main() {filepath := "C:\\Users\\YangYi\\Desktop\\动作.txt"content ,err :=ioutil.ReadFile(filepath)  // 直接读取filepath文件中的内容if err !=nil {log.Panic(err)}fmt.Println(string(content))
}
2 按字节读取文件

参考地址:https://segmentfault.com/a/1190000017918542

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com