浪花一朵朵歌曲下载:Go 语言文件操作示例

来源:百度文库 编辑:中财网 时间:2024/05/08 16:43:40

[代码] 关闭文件

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

[代码] 文件读取

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

[代码] 写文件

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

[代码] 获取文件名

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