go语言基础 写出数据 Writer() os包

栏目: Go · 发布时间: 6年前

内容简介:版权声明: https://blog.csdn.net/weixin_42100098/article/details/80147933

版权声明: https://blog.csdn.net/weixin_42100098/article/details/80147933

第一步 首先打开一个文件,如果该文件不存在就创建一个,并且写入权限

OpenFile(filename,flag,perm)-->*File,err  *****

filename:文件名字 

flag,获取file的打开模式:

O_RDONLY  只读

O_WRONLY  只写

O_RDWR      可读可写

O_CREATE    创建文件

perm,如果文件不存在,需要指定创建的文件的权限

0777(可读可写)

-rw-

第二步     数据写出

Write([]byte)-->count,err,写出字节数组

WriteAt([]byte,off),在指定位置写出数据。

WriteString(string),直接写出字符串

package main

import (
   "os"
   "fmt"
)

func main()  {
   /*
   Writer(),向外写数据
    */
    file,_:=os.OpenFile("C:\\liu\\pro\\ao.txt",os.O_WRONLY|os.O_CREATE,0777)
    defer file.Close()
    //直接写出字节数组、



    bs := [] byte{97,98,99,100,101,102} // a-f
    file.Write(bs[0:4]) //0123
   file.WriteString("面朝大海")
   //abcd面朝大海
   /*
   16个字节
   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
   a b c d   面    朝      大      海
   abcd面朝大ABCDE
    */


   file.WriteAt([]byte{'A','B','C','D','E'},13)
    fmt.Println("写完啦啊。。")




}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Head First jQuery

Head First jQuery

Ryan Benedetti , Ronan Cranley / O'Reilly Media / 2011-9 / USD 39.99

Want to add more interactivity and polish to your websites? Discover how jQuery can help you build complex scripting functionality in just a few lines of code. With Head First jQuery, you'll quickly g......一起来看看 《Head First jQuery》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具