This is a creation in Article, where the information may have evolved or changed. Recently write a file upload function, in view of their own experience of the go language is not enough, so the writing may be very general, but also please give us a lot of advice <br/> talk, directly on the code "" "Package Mainimport (" FMT "" html/template "IO" "Sync" "Log" "Net/http" "OS" "Time") type Buffer struct {bit []byteblen int}var bufpool *sync. Poolfunc INITBT () {Bufpool = &sync. Pool{}bufpool. New = func () interface{} {return Buffer{bit:make ([]byte, 64*1024), Blen:0,}}}func Copy (dst1 io. Writer, src io. Reader, FD *os. File) (err error) {ta: = time. Now (). Unixnano () var cache chan *buffer = make (chan *buffer, 16*1024) var tb int64 = 0go func (fd1 *os. File) {for {select {case BUF: = <-cache:if BUF = = Nil {goto END}NW1, EW: = Dst1. Write ((*buf). Bit[0: (*BUF). Blen]) if ew! = Nil {err: = ewfmt. Println ("-----------", err) goto end}if (*BUF). Blen! = NW1 {err: = io. ERRSHORTWRITEFMT.PRINTLN (Err) goto End}bufpool. Put ((*BUF))}}end:t2: = time. Now (). Unixnano () fmt. Println ("Eg:", T2-ta) fd1. Close ()} (FD) for {buffer: = Bufpool. Get (). (Buffer) nr, er: = src. Read (buffer.bit) if er! = nil {if er = = io. EOF {Close (cache) break}err = Er}buffer.blen = Nrcache <-&BUFFER}TB = time. Now (). Unixnano () fmt. Println ("read:", Tb-ta) return Err}func upload (w http. Responsewriter, R *http. Request) {r.parseform () if R.method = = "GET" {t, err: = template. Parsefiles ("UPLOAD.GPTL") 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) T1: = time. Now (). Unixnano () Copy (f, file, F) checkerr (ERR) defer file. Close () T2: = time. Now (). Unixnano () fmt. Printf ("Sum:%v\n", T2-t1) fmt. fprintf (W, "Upload over")}}func Checkerr (err error) {if err! = Nil {err. Error ()}}func main () {INITBT () http. Handlefunc ("/upload", upload) fmt. Println ("open") Err: = http. Listenandserve (": 8888", nil) if err! = Nil {log. Fatal ("Listenandserve:", Err)}} "The file crosses, mainly using a new gorotine to perform the write operation of the file, and then pass through Chan to carry the pointer containing the file data,<br/> Self-feeling upload performance improved, also ask the great God to give good advice 374 reads ∙ 1 likes
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