package main

import (
"fmt"
"io/ioutil"
"os"
) func main() {
filename := "a.txt"
//--------- create file ----------
file, err := os.Create(filename)
if err != nil {
fmt.Println(err)
}
defer file.Close() str := "你好,世界"
data := []byte(str) err = ioutil.WriteFile(filename, data, ) //--------- read file ------------
buf := make([]byte, )
n, err := file.Read(buf)
if err != nil {
fmt.Println(err)
}
fmt.Printf("------------method 1-------------\n%v\n", buf)
fmt.Printf("--> 读取长度:%v\n", n)
fmt.Printf("--> %v,\n", string(buf)) //------------ read file method 2 ---------
res, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println(err)
}
fmt.Println("---------- method 2----------")
fmt.Println(string(res)) }

结果:

$ go run main.go
------------method -------------
[ ]
--> 读取长度:
--> 你好,世界,
---------- method ----------
你好,世界

go语言读写文件的更多相关文章

  1. C++封装C语言读写文件

    自己项目需要,封装C语言读写文件. 为了兼容低版本的编译器,因为低版本的编译器(比如,Vs2010,Vs2008)他们可能不支持 modern c++. 项目 使用 cmake管理的项目. 可以在 g ...

  2. C语言读写文件

    对文件的读和写是最常用的文件操作.在C语言中提供了多种文件读写的函数: 字符读写函数  :fgetc和fputc 字符串读写函数:fgets和fputs 数据块读写函数:freed和fwrite 格式 ...

  3. 如何用C语言读写文件

    #include "stdio.h"#include <stdlib.h> main(){ FILE *fp1;//定义文件流指针,用于打开读取的文件 FILE *fp ...

  4. 第28月第3天 c语言读写文件

    1. int ConfigIniFile::OpenFile( const char* szFileName ) { FILE *fp; size_t nLen; int nRet; CloseFil ...

  5. 「C语言」文件的概念与简单数据流的读写函数

    写完「C语言」单链表/双向链表的建立/遍历/插入/删除 后,如何将内存中的链表信息及时的保存到文件中,又能够及时的从文件中读取出来进行处理,便需要用到”文件“的相关知识点进行文件的输入.输出. 其实, ...

  6. C语言基础文件读写操作

    整理了一份C语言的文件读写件操作代码,测试时打开相应的注释即可. #include <stdio.h> #include <stdlib.h> #include <uni ...

  7. R语言读写中文编码方式

    最近遇到一个很头疼的事,就是 R语言读写中文编码方式.在网上找到了一篇博文,谢谢博主的精彩分享,让我很快解决了问题,在此也分享一下 R语言读写数据的方法很多,这里主要是我在使用read.csv/rea ...

  8. Inno Setup 如何读写文件

    软件安装的实质就是拷贝,对于简单的打包当然不需要考虑修改某(配置)文件.通过inno修改文件的目的在于把安装时相关信息写入文件中,提供其它应用的读取,而这些信息也只能在安装时才能确定,比如安装用户选择 ...

  9. php中并发读写文件冲突的解决方案(文件锁应用示例)

    PHP(外文名: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,入门门槛较低,易于学习,使用广泛,主要适 ...

随机推荐

  1. Linux 中 /proc/kcore为啥如此之大

    What Is /proc/kcore?None of the files in /proc are really there--they're all, "pretend," f ...

  2. Java判断对象类型是否为数组

    判断对象是否为数组: public static void main(String[] args) { String[] a = ["1","2"]; if(a ...

  3. yarn cluster和yarn client模式区别——yarn-cluster适用于生产环境,结果存HDFS;而yarn-client适用于交互和调试,也就是希望快速地看到application的输出

    Yarn-cluster VS Yarn-client 从广义上讲,yarn-cluster适用于生产环境:而yarn-client适用于交互和调试,也就是希望快速地看到application的输出. ...

  4. redis都有哪些数据类型?分别在哪些场景下使用比较合适?

    (1)string 这是最基本的类型了,没啥可说的,就是普通的set和get,做简单的kv缓存 (2)hash 这个是类似map的一种结构,这个一般就是可以将结构化的数据,比如一个对象(前提是这个对象 ...

  5. 十七.protobuf在rpc中的使用

    关于protobuf在rpc中的使用,设计到gRPC,相关内容待续....

  6. C# URL编码转换 URL转码 UrlDecode UrlEncode

    using System.Web; 引用system.web. textBox2.Text = System.Web.HttpUtility.UrlDecode(textBox1.Text, Syst ...

  7. How to fix “Internal Diagnostics Hub Exception” in VS 2015?

    This worked for me: Stop the VSHub.exe process Delete the files in %TMP%\VsHub\ Restart the "Vi ...

  8. Java - 基础到进阶

    # day01 一:基本操作 package _01.java基本操作; /** * 文档注释 */ public class _01Alls { public static void main(St ...

  9. c# 隐藏窗口在ALT+TAB中

    winform: protected override CreateParams CreateParams { get { const int WS_EX_APPWINDOW = 0x40000; c ...

  10. BZOJ 2159: Crash 的文明世界 第二类斯特林数+树形dp

    这个题非常巧妙啊~ #include <bits/stdc++.h> #define M 170 #define N 50003 #define mod 10007 #define LL ...