"Go" Golang file operations read and write files, upload files, traverse files, delete files

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

1. Create and Open
To create a file function:
Func Create (name string) (file *file, err Error)
Func NewFile (fd int, name string) *file
To open a file function:
Func Open (name string) (file *file, err Error)
Func OpenFile (name string, flag int, perm uint32) (file *file, err Error)

2. Writing files
Write file function:
Func (file *file) Write (b []byte) (n int, err Error)
Func (file *file) writeat (b []byte, off Int64) (n int, err Error)
Func (file *file) writestring (S string) (ret int, err Error)

Instance:
Package Main
Import (
"OS"
"FMT"
)
Func Main () {
UserFile: = "Test.txt"
Fout,err: = os. Create (UserFile)
Defer Fout. Close ()
If err! = Nil {
Fmt. Println (Userfile,err)
Return
}
For i:= 0;i<10;i++ {
Fout. WriteString ("www.godeye.org!\r\n")
Fout. Write ([]byte ("www.godeye.org!\r\n"))
}
}

3. Read the file
Read File function:
Func (file *file) Read (b []byte) (n int, err Error)
Func (file *file) readat (b []byte, off Int64) (n int, err Error)
Instance:
Package Main

Import (
"FMT"
"OS"
)

Func Main () {
UserFile: = "Test.txt"
Fin, err: = OS. Open (UserFile)
Defer fin. Close ()
If err! = Nil {
Fmt. Println (UserFile, err)
Return
}
BUF: = Make ([]byte, 1024)
for {
N, _: = Fin. Read (BUF)
if 0 = = n {
Break
}
Os. Stdout.write (Buf[:n])
}
}

4. deleting files
Go language inside Delete file and delete folder is the same function
Func Remove (name string) Error
Instance:
Package Main

Import (
"OS"
)

Func Main () {
UserFile: = "Godeye.txt"
Os. Remove (UserFile)
}

5. Traverse a Folder
Package Main
Import (
"Flag"
"FMT"
"OS"
"Path/filepath"
)

Func getfilelist (path string) {
ERR: = FilePath. Walk (Path, func (path string, f OS). FileInfo, err Error) error {
if f = = nil {
return err
}
If F.isdir () {
return Nil
}
Fmt. Printf (PATH)
return Nil
})
If err! = Nil {
Fmt. Printf ("filepath. Walk () returned%v\n ", err)
}
}
Func Main () {
Getfilelist ("src")
}

6. classic File Upload
Here are 2 files, one is the server handler Upload.go, and the other is the template page upload.html

See Upload.go first.
Package Main

Import (
"FMT"
"Html/template"
"IO"
"Log"
"Net/http"
"OS"
)

var buf []byte

Func Upload (w http. Responsewriter, R *http. Request) {
R.parseform ()
if R.method = = "GET" {
T, err: = template. Parsefiles ("upload.html")
Checkerr (ERR)
T.execute (W, Nil)
} else {
File, handle, err: = R.formfile ("file")
Checkerr (ERR)
F, err: = OS. OpenFile ("./test/" +handle. Filename, OS. O_wronly|os. O_create, 0666)
Io. Copy (f, file)
Checkerr (ERR)
Defer F.close ()
Defer file. Close ()
Fmt. Println ("Upload success")
}
}

Func Checkerr (err error) {
If err! = Nil {
Err. Error ()
}
}

Func Main () {
http. Handlefunc ("/upload", upload)
ERR: = http. Listenandserve (": 8888", nil)
If err! = Nil {
Log. Fatal ("Listenandserve:", err)
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.