Golang文件IO 一
Posted majianguo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Golang文件IO 一相关的知识,希望对你有一定的参考价值。
Golang文件IO 一
文件IO编程最基本、最常用的就属读写文件操作了。ioutil包实现了一些IO实用功能,其中就包括非常简捷、好用的文件读取功能。
ioutil包有7个函数1个变量:
var Discard io.Writer = devNull(0)
func NopCloser(r io.Reader) io.ReadCloser
func ReadAll(r io.Reader) ([]byte, error)
func ReadDir(dirname string) ([]os.FileInfo, error)
func ReadFile(filename string) ([]byte, error)
func TempDir(dir, prefix string) (name string, err error)
func TempFile(dir, prefix string) (f *os.File, err error)
func WriteFile(filename string, data []byte, perm os.FileMode) error
ReadFile和WriteFile这两个函数分别实现了读文件和写文件操作,一行代码即可搞定,非常便捷。
func ReadFile(filename string) ([]byte, error)
ReadFile读取filename指定的文件,返回文件的内容。如果调用成功返回err==nil,而不是err==EOF。因为ReadFile读取整个文件,不会将Read操作遇到的EOF报告为一个错误。
func WriteFile(filename string, data []byte, perm os.FileMode) error
WriteFile将data写入到filename指定的文件。如果文件不存在,WriteFile用权限perm创建文件;如果文件存在,WriteFile函数在写之前先将文件截断。
可以搭配json包实现程序配置的读写功能。下面是一个读写数据库配置的例子。
package main import ( "fmt" "io/ioutil" "log" "encoding/json" ) type AppConf struct{ DriverName string `json:"driver_name"` SqlUser string `json:"sql_user"` SqlPass string `json:"sql_password"` SqlUrls string `json:"sql_urls"` SqlDB string `json:"sql_db"` } func main() { appConf := AppConf{DriverName:"mysql", SqlUser:"root", SqlPass:"password", SqlUrls:"tcp(127.0.0.1:3306)", SqlDB:"mydb"} bConf,err := json.MarshalIndent(appConf,"","\\t") if err != nil { log.Fatal(err) } err = ioutil.WriteFile("app.cfg",bConf,0666) if err != nil { log.Fatal(err) } var appConf2 AppConf bConf,err =ioutil.ReadFile("app.cfg") if err != nil { log.Fatal(err) } err = json.Unmarshal(bConf,&appConf2) if err != nil { log.Fatal(err) } fmt.Println("appConf === appConf2",appConf==appConf2) }
运行结果如下:
生成的app.cfg内容如下:
{ "driver_name": "mysql", "sql_user": "root", "sql_password": "password", "sql_urls": "tcp(127.0.0.1:3306)", "sql_db": "mydb" }
通常WriteFile的权限是0666。关于权限的定义,可以查看golang源码(C:\\Go\\src\\os\\types.go)
// A FileMode represents a file\'s mode and permission bits. // The bits have the same definition on all systems, so that // information about files can be moved from one system // to another portably. Not all bits apply to all systems. // The only required bit is ModeDir for directories. type FileMode uint32 // The defined file mode bits are the most significant bits of the FileMode. // The nine least-significant bits are the standard Unix rwxrwxrwx permissions. // The values of these bits should be considered part of the public API and // may be used in wire protocols or disk representations: they must not be // changed, although new bits might be added. const ( // The single letters are the abbreviations // used by the String method\'s formatting. ModeDir FileMode = 1 << (32 - 1 - iota) // d: is a directory ModeAppend // a: append-only ModeExclusive // l: exclusive use ModeTemporary // T: temporary file; Plan 9 only ModeSymlink // L: symbolic link ModeDevice // D: device file ModeNamedPipe // p: named pipe (FIFO) ModeSocket // S: Unix domain socket ModeSetuid // u: setuid ModeSetgid // g: setgid ModeCharDevice // c: Unix character device, when ModeDevice is set ModeSticky // t: sticky // Mask for the type bits. For regular files, none will be set. ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice ModePerm FileMode = 0777 // Unix permission bits )
FileMode在所有系统种的定义相同,与linux系统的权限定义类似,一些可取的值包括如下:
const ( OS_READ = 04 OS_WRITE = 02 OS_EX = 01 OS_USER_SHIFT = 6 OS_GROUP_SHIFT = 3 OS_OTH_SHIFT = 0 OS_USER_R = OS_READ<<OS_USER_SHIFT OS_USER_W = OS_WRITE<<OS_USER_SHIFT OS_USER_X = OS_EX<<OS_USER_SHIFT OS_USER_RW = OS_USER_R | OS_USER_W OS_USER_RWX = OS_USER_RW | OS_USER_X OS_GROUP_R = OS_READ<<OS_GROUP_SHIFT OS_GROUP_W = OS_WRITE<<OS_GROUP_SHIFT OS_GROUP_X = OS_EX<<OS_GROUP_SHIFT OS_GROUP_RW = OS_GROUP_R | OS_GROUP_W OS_GROUP_RWX = OS_GROUP_RW | OS_GROUP_X OS_OTH_R = OS_READ<<OS_OTH_SHIFT OS_OTH_W = OS_WRITE<<OS_OTH_SHIFT OS_OTH_X = OS_EX<<OS_OTH_SHIFT OS_OTH_RW = OS_OTH_R | OS_OTH_W OS_OTH_RWX = OS_OTH_RW | OS_OTH_X OS_ALL_R = OS_USER_R | OS_GROUP_R | OS_OTH_R OS_ALL_W = OS_USER_W | OS_GROUP_W | OS_OTH_W OS_ALL_X = OS_USER_X | OS_GROUP_X | OS_OTH_X OS_ALL_RW = OS_ALL_R | OS_ALL_W OS_ALL_RWX = OS_ALL_RW | OS_GROUP_X)
WriteFile权限指定为0777,Ubuntu上执行上面的例子,查看app.cfg的详细信息
dell@dell-VirtualBox:~/mygo/src/example/ioutil$ ll app.cfg -rwxrwxr-x 1 dell dell 132 11月 21 22:15 app.cfg*
WriteFile 权限指定为0666,Ubuntu上执行上面的例子,查看app.cfg的详细信息
dell@dell-VirtualBox:~/mygo/src/example/ioutil$ ll app.cfg -rw-rw-r-- 1 dell dell 132 11月 21 22:18 app.cfg
以上是关于Golang文件IO 一的主要内容,如果未能解决你的问题,请参考以下文章