分享

Go 语言文件操作示例

 MikeDoc 2012-02-09

[代码] 关闭文件

01func (file *File) Close() os.Error {
02    if file == nil {
03        return os.EINVAL
04    }
05    e := syscall.Close(file.fd)
06    file.fd = -1 // so it can't be closed again
07    if e != 0 {
08        return os.Errno(e)
09    }
10    return nil
11}

[代码] 文件读取

01func (file *File) Read(b []byte) (ret int, err os.Error) {
02    if file == nil {
03        return -1, os.EINVAL
04    }
05    r, e := syscall.Read(file.fd, b)
06    if e != 0 {
07        err = os.Errno(e)
08    }
09    return int(r), err
10}

[代码] 写文件

01func (file *File) Write(b []byte) (ret int, err os.Error) {
02    if file == nil {
03        return -1, os.EINVAL
04    }
05    r, e := syscall.Write(file.fd, b)
06    if e != 0 {
07        err = os.Errno(e)
08    }
09    return int(r), err
10}

[代码] 获取文件名

1func (file *File) String() string {
2    return file.name
3}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多