This is a creation in Article, where the information may have evolved or changed.
"Disclaimer: Copyright All, welcome reprint, do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "
A friend with experience in Python web development. I believe the convenience of it must be very impressive.
In fact, the use of the Go language to the Web site development is also very easy to do things.
I had a 0 experience with web development before. But with the go language, you can build a site in the shortest time possible.
For the convenience of learning. You can download the entire code from GitHub directly to this blog. At the same time, the Code section in the article refers to the code in the Go Language program, which is a tribute. The address of this content is here. Interested students can download a look.
From the folder, the contents of the code are simple. Picture.go includes all of the interactive code, and list.html and upload.html include the template files used. The Uploads folder saves all uploaded image files.
First look at the Picture.go code content,
Package Mainimport "IO" import "Log" import "OS" import "net/http" import "html/template" import "Io/ioutil" const (UPLOAD_ DIR = "./uploads") Func Uploadhandler (w http. Responsewriter, R * http. Request) {if R.method = = "GET" {T, _: = template. Parsefiles ("upload.html") T.execute (W, nil)}else {f, h, _: = R.formfile ("image") FileName: = H.filenamedefer f.close () T, _ : = OS. Create (Upload_dir + "/" + filename) defer t.close () _, Err: = Io. Copy (T, f) if err! = Nil {return}http. Redirect (W, R, "view?")id= "+ filename, http. Statusfound)}}func Viewhandler (w http. Responsewriter, r* http. Request) {imageId: = R.formvalue ("id") ImagePath: = Upload_dir + "/" + Imageidw.header (). Set ("Content-type", "image") http. Servefile (W, R, ImagePath)}func Listhandler (w http. Responsewriter, r* http. Request) {Fileinfoarr, _: = Ioutil. ReadDir (upload_dir) Locals: = Make (map[string] interface{}) Images: = []string{}for _, FileInfo: = range Fileinfoarr {image s = append (images, fileinfo.name ())}locals["Images"] = Imagest, _: = template. Parsefiles ("list.html") T.execute (W, locals)}func main () {http. Handlefunc ("/upload", Uploadhandler) http. Handlefunc ("/view", Viewhandler) http. Handlefunc ("/", Listhandler) Err: = http. Listenandserve (": 9090", nil) if err! = Nil {log. Fatal ("Listenandserve:", err.) Error ())}}
In fact, this site mainly on 3 pages. One is the index that displays all the pictures. One is the picture display, the other is the picture upload page.
See below. What are the contents of upload.html?
<!doctype Html>
A friend with a front-end development experience must have seen it at a glance, which is actually a simple login upload page. So what is list.html again?
<!doctype html>
The above page is more a Web page than that. Rather, it's a template. Since all the images content is actually transferred from the outside world, all this content will form a real Web page. I don't know if I said it clearly.
The above site is simple and clear, and interested friends can have a good look.
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.