Files
build-web-application-with-…/en/eBook/07.5.md
2014-05-10 18:05:31 +08:00

159 lines
3.2 KiB
Markdown

# 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.
## Directories
Most of functions of file operations is in package `os`, here are some functions about directories:
- func Mkdir(name string, perm FileMode) error
Create directory with `name`, `perm` is permission, like 0777.
- func MkdirAll(path string, perm FileMode) error
Create multiple directories according to `path`, like `astaxie/test1/test2`.
- func Remove(name string) error
Remove directory with `name`, it returns error if it's not 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.
Code sample:
package main
import (
"fmt"
"os"
)
func main() {
os.Mkdir("astaxie", 0777)
os.MkdirAll("astaxie/test1/test2", 0777)
err := os.Remove("astaxie")
if err != nil {
fmt.Println(err)
}
os.RemoveAll("astaxie")
}
## Files
### Create and open files
Two functions to create files:
- func Create(name string) (file *File, err Error)
Create file with `name` and return a file object with permission 0666 and read-writable.
- func NewFile(fd uintptr, name string) *File
Create file and return a file object.
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.
- 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.
### Write files
Functions for writing files:
- func (file *File) Write(b []byte) (n int, err Error)
Write byte type content to file.
- func (file *File) WriteAt(b []byte, off int64) (n int, err Error)
Write byte type content to certain position of file.
- func (file *File) WriteString(s string) (ret int, err Error)
Write string to file.
Code sample:
package main
import (
"fmt"
"os"
)
func main() {
userFile := "astaxie.txt"
fout, err := os.Create(userFile)
if err != nil {
fmt.Println(userFile, err)
return
}
defer fout.Close()
for i := 0; i < 10; i++ {
fout.WriteString("Just a test!\r\n")
fout.Write([]byte("Just a test!\r\n"))
}
}
### Read files
Functions for reading files:
- func (file *File) Read(b []byte) (n int, err Error)
Read data to `b`.
- func (file *File) ReadAt(b []byte, off int64) (n int, err Error)
Read data from position `off` to `b`.
Code sample:
package main
import (
"fmt"
"os"
)
func main() {
userFile := "asatxie.txt"
fl, err := os.Open(userFile)
if err != nil {
fmt.Println(userFile, err)
return
}
defer fl.Close()
buf := make([]byte, 1024)
for {
n, _ := fl.Read(buf)
if 0 == n {
break
}
os.Stdout.Write(buf[:n])
}
}
### Delete files
Go uses same function for removing files and directories:
- func Remove(name string) Error
Remove file or directory with `name`.( ***`name` ends with `/` means directory*** )
## Links
- [Directory](preface.md)
- Previous section: [Templates](07.4.md)
- Next section: [Strings](07.6.md)