The example in this article describes the way the go language file operates. Share to everyone for your reference. Specifically as follows:
To close a file:
Copy Code code as follows:
Func (file *file) close () OS. Error {
If file = = Nil {
return OS. Einval
}
E: = Syscall. Close (FILE.FD)
FILE.FD =-1//So it can ' t be closed again
If e!= 0 {
return OS. Errno (e)
}
return Nil
}
File reads:
Copy Code code as follows:
Func (file *file) Read (b []byte) (ret int, err OS.) Error) {
If file = = Nil {
Return-1, OS. Einval
}
R, E: = Syscall. Read (FILE.FD, B)
If e!= 0 {
Err = OS. Errno (e)
}
return int (r), err
}
Write file:
Copy Code code as follows:
Func (file *file) Write (b []byte) (ret int, err OS.) Error) {
If file = = Nil {
Return-1, OS. Einval
}
R, E: = Syscall. Write (FILE.FD, B)
If e!= 0 {
Err = OS. Errno (e)
}
return int (r), err
}
Get file name:
Copy Code code as follows:
Func (file *file) string () string {
Return File.name
}
I hope this article will help you with your go language program.