Merge commit '4afdafd964e8e32c1e12a342e3ff73d1f42a9148' into ja
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
# 7.5 Files
|
||||
|
||||
File is must-have object in every single computer device, and web applications also have many usage with files. In this section, we're going to learn how to operate files in Go.
|
||||
Files are must-have objects on every single computer device. It won't come as any surprise to you that web applications also make heavy use of them. In this section, we're going to learn how to operate on files in Go.
|
||||
|
||||
## Directories
|
||||
|
||||
Most of functions of file operations is in package `os`, here are some functions about directories:
|
||||
In Go, most of the file operation functions are located in the `os` package. Here are some directory functions:
|
||||
|
||||
- func Mkdir(name string, perm FileMode) error
|
||||
|
||||
Create directory with `name`, `perm` is permission, like 0777.
|
||||
Create a directory with `name`. `perm` is the directory permissions, i.e 0777.
|
||||
|
||||
- func MkdirAll(path string, perm FileMode) error
|
||||
|
||||
@@ -16,11 +16,11 @@ Most of functions of file operations is in package `os`, here are some functions
|
||||
|
||||
- func Remove(name string) error
|
||||
|
||||
Remove directory with `name`, it returns error if it's not directory or not empty.
|
||||
Removes directory with `name`. Returns error if it's not a directory or not empty.
|
||||
|
||||
- func RemoveAll(path string) error
|
||||
|
||||
Remove multiple directories according to `path`, it will not be deleted if `path` is a single path.
|
||||
Removes multiple directories according to `path`. Directories will not be deleted if `path` is a single path.
|
||||
|
||||
Code sample:
|
||||
|
||||
@@ -45,26 +45,26 @@ Code sample:
|
||||
|
||||
### Create and open files
|
||||
|
||||
Two functions to create files:
|
||||
There are two functions for creating files:
|
||||
|
||||
- func Create(name string) (file *File, err Error)
|
||||
|
||||
Create file with `name` and return a file object with permission 0666 and read-writable.
|
||||
Create a file with `name` and return a read-writable file object with permission 0666.
|
||||
|
||||
- func NewFile(fd uintptr, name string) *File
|
||||
|
||||
Create file and return a file object.
|
||||
Create a file and return a file object.
|
||||
|
||||
|
||||
Two functions to open files:
|
||||
There are also two functions to open files:
|
||||
|
||||
- func Open(name string) (file *File, err Error)
|
||||
|
||||
Open file with `name` with read-only permission, it calls `OpenFile` underlying.
|
||||
Opens a file called `name` with read-only access, calling `OpenFile` under the covers.
|
||||
|
||||
- func OpenFile(name string, flag int, perm uint32) (file *File, err Error)
|
||||
|
||||
Open file with `name`, `flag` is open mode like read-only, read-write, `perm` is permission.
|
||||
Opens a file called `name`. `flag` is open mode like read-only, read-write, etc. `perm` are the file permissions.
|
||||
|
||||
### Write files
|
||||
|
||||
@@ -72,15 +72,15 @@ Functions for writing files:
|
||||
|
||||
- func (file *File) Write(b []byte) (n int, err Error)
|
||||
|
||||
Write byte type content to file.
|
||||
Write byte type content to a file.
|
||||
|
||||
- func (file *File) WriteAt(b []byte, off int64) (n int, err Error)
|
||||
|
||||
Write byte type content to certain position of file.
|
||||
Write byte type content to a specific position of a file.
|
||||
|
||||
- func (file *File) WriteString(s string) (ret int, err Error)
|
||||
|
||||
Write string to file.
|
||||
Write a string to a file.
|
||||
|
||||
Code sample:
|
||||
|
||||
@@ -146,14 +146,14 @@ Code sample:
|
||||
|
||||
### Delete files
|
||||
|
||||
Go uses same function for removing files and directories:
|
||||
Go uses the same function for removing files and directories:
|
||||
|
||||
- func Remove(name string) Error
|
||||
|
||||
Remove file or directory with `name`.( ***`name` ends with `/` means directory*** )
|
||||
Remove a file or directory called `name`.( *** a `name` ending with `/` signifies that it's a directory*** )
|
||||
|
||||
## Links
|
||||
|
||||
- [Directory](preface.md)
|
||||
- Previous section: [Templates](07.4.md)
|
||||
- Next section: [Strings](07.6.md)
|
||||
- Next section: [Strings](07.6.md)
|
||||
|
||||
Reference in New Issue
Block a user