?yb? 2012-11-22 13:52 采纳率: 100%
浏览 173
已采纳

如何将整个文件读入字符串变量

I have lots of small files, I don't want to read them line by line.

Is there a function in Go that will read a whole file into a string variable?

转载于:https://stackoverflow.com/questions/13514184/how-can-i-read-a-whole-file-into-a-string-variable

  • 写回答

4条回答 默认 最新

  • MAO-EYE 2012-11-22 14:04
    关注

    Use ioutil.ReadFile:

    func ReadFile(filename string) ([]byte, error)
    

    ReadFile reads the file named by filename and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads the whole file, it does not treat an EOF from Read as an error to be reported.

    You will get a []byte instead of a string. It can be converted if really necessary:

    s := string(buf)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 绿盟安全扫描--检测到目标站点存在javascript框架库漏洞
  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包