Go_os

u012807459 · · 3921 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

这两天阅读了下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就会收到内容


有疑问加站长微信联系(非本文作者)

本文来自:CSDN博客

感谢作者:u012807459

查看原文:Go_os

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

3921 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传