This is a creation in Article, where the information may have evolved or changed.
Upload.go
package mainimport ("Crypto/md5" "FMT" "html/template" "io" "Log" "Net/http" "OS" StrConv "" Time ")//Handle/upload logical func upload (w http. Responsewriter, R *http. Request) {fmt. Println ("Method:", R.method)//Gets the requested method if R.method = = "Get" {crutime: = time. Now (). Unix () H: = MD5. New () Io. WriteString (H, StrConv. Formatint (Crutime, Ten)) token: = Fmt. Sprintf ("%x", H.sum (nil)) T, _: = template. Parsefiles ("Upload.gtpl") T.execute (W, token)} else {r.parsemultipartform (<<) file, handler, err: = R. Formfile ("UploadFile") if err! = Nil {fmt. PRINTLN (Err) return}defer file. Close () fmt. fprintf (W, "%v", handler. Header) F, err: = OS. OpenFile ("./upload/" +handler. Filename, OS. O_wronly|os. O_create, 0666)///This assumes that the upload directory already exists under the current directory if err! = Nil {fmt. PRINTLN (Err) return}defer f.close () io. Copy (f, file)}}func Main () {http. Handlefunc ("/upload", upload)//set access to the route err: = http. Listenandserve (": 9090", nil)//Set the listening port if err! = Nil {log. Fatal ("Listenandserve:", Err)}}
Upload.gtpl