• Golang ioutil读写文件测试


    运用 ioutil.ReadFile 、ioutil.WriteFile

    package main
    
    import (
    	"io/ioutil"
    	"log"
    	"os"
    )
    
    func main() {
    
    	buf, err := ioutil.ReadFile("C:/文档/MHOSO/conf/conf.txt")
    	if err != nil {
    		log.Println(err)
    	}
    	log.Println(string(buf))
    	log.Println()
    
    	//buf.append('8')
    	/*newBuf := make([]byte, len(buf)*5)
    	copy(newBuf, buf)
    	log.Println(string(newBuf))
    	log.Println()
    	buf = newBuf*/
    
    	str := []byte("this is the new word")
    	log.Println(string(str))
    	log.Println()
    
    	buf = append(buf, str...)
    	//buf := []byte("hi this is test")
    	ioutil.WriteFile("C:/文档/MHOSO/conf/conf.txt", buf, os.ModeAppend)
    	log.Println(string(buf))
    }
    

    *** 输出结果比较奇怪,不知道是不是内存问题:

    [Running] go run "c:UserscjigeDesktop	est.go"
    2018/12/03 20:50:11 hi this is test2018/12/03 20:50:11 hi this is test
    [Done] exited with code=0 in 1.647 seconds
    

    更换文件位置后,测试结果如下,append没有成功

    [Running] go run "c:文档Go学习文件读写.go"
    2018/12/03 20:54:17 hi this is test2018/12/03 20:54:17 
    2018/12/03 20:54:17 this is the new word
    2018/12/03 20:54:17 
    2018/12/03 20:54:17 hi this is test
    [Done] exited with code=0 in 1.542 seconds
    
  • 相关阅读:
    redis数据持久化
    redis安全:给redis设置密码
    redis命令总结
    redis事务
    redis发布订阅
    Java中的日期
    链式队列
    删除链表中的结点(链表)、比较含退格的字符串(栈)、棒球比赛(栈)
    物理层
    链式栈
  • 原文地址:https://www.cnblogs.com/boxker/p/10060824.html
Copyright © 2020-2023  润新知