Go_os

来源:互联网 发布:医药b2b平台源码 编辑:程序博客网 时间:2024/06/08 00:26

这两天阅读了下Golang的标准包os的文档把里面的方法看了个大概

记录一些比较容易混淆的

os.Lstat() os.Stat()
两者都是返回FileInfo 唯一的区别是如果是一个 symbolic link name前者返回的是
symbolic link 的FileInfo  后者则返回symbolic link 所指向文件的FileInfo

os.Sync()
将要写入磁盘的内容立即提交 写入到磁盘
在StackOverflow上找到的一段话 解释的不错
You'll notice that an os.File doesn't have a .Flush() because it doesn't need one because it isn't buffered. Writes to it are direct syscalls to write to the file.
When your program exits(even if it crashes) all files it has open will be closed automatically by the operating system and the file system will write your changes to disk when it gets around to it (sometimes up to few minutes after your program exits).
Calling os.File.Sync() will call the fsync() syscall which will force the file system to flush it's buffers to disk. This will guarantee that your data is on disk and persistent even if the system is powered down or the operating system crashes.
You don't need to call .Sync()

os.Process 进程结构体
func (p *Process) Wait() (*ProcessState, error) 
这个函数用于等待p进程 只有当p进程结束时函数才可以继续执行

os.ProcessState 进程状态的结构体
func (p *ProcessState) SystemTime() time.Duration
可以查看进程使用CPU的时间

os/exec包下面
Cmd struct 可以看做一个命令行窗口 甚至 使用方式都是一模一样的
func Command(name string, arg ...string) *Cmd
这是一个启动一个程序的方法
比如
cmd := os.Command("ls","-la")
//指定输出到哪里
cmd.Stdout = os.Stdout
这与在命令行窗口下使用ls -la效果一模一样

func (c *Cmd) StderrPipe() (io.ReadCloser, error)
返回一个连接到标准错误输出的管道上 当command启动之后如果有错误将会输出到这个管道上
func (c *Cmd) StdoutPipe() (io.ReadCloser, error)
返回一个连接到标准输出的管道上 当command启动之后如果有输出将会输出到这个管道上
func (c *Cmd) StdinPipe() (io.WriteCloser, error)
返回一个连接到一个标准输入管道 当通过该管道输入内容时 Cmd.Stdout就会收到内容

0 0