The Magic Go Language (image website development)

Source: Internet
Author: User


"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "


A friend with experience in Python web development, I'm sure it's very impressive for its convenience. In fact, the use of the go language of the Web site development is also very easy thing. I've had a 0 experience with web development before, but with the go language, you can build a website in the shortest time possible.

For the convenience of learning, you can download it directly from GitHub to all the code mentioned in this blog post. At the same time, the Code section of the article refers to the code content in Go language programming, and here is a thank you. The content of the address in this, interested students can download a look.


From the table of contents, the contents of the code are very simple. Picture.go contains all the interaction code, list.html and upload.html contain the template file used, and the uploads directory holds 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 to display the index of all the pictures, one is the picture display, the other is the picture upload page.


Let's see, what are the contents of upload.html?

<!doctype Html>

Have a front-end development experience of the friends must be a glance, this is actually a simple login upload page. So what is list.html again?

<!doctype html>

The above page is more of a template than a Web page. Because all of the images content is actually to be transmitted from the outside world, and all this content will constitute a real web page, do not know I said clearly no.


The website above is simple and clear, and interested friends can have a good look.




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.