Magic go language (image website development), image website development

Source: Internet
Author: User

Magic go language (image website development), image website development


[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]


I believe that I am very impressed with the convenience of python web development. In fact, using the go language to develop web sites is also very easy. My previous experience in web development is also 0, but after using the go language, you can build a website in the shortest time.

For ease of learning, you can download all the Code mentioned in this blog from github. At the same time, the Code section in the article references the code content in go language programming, and I would like to express my gratitude here. Here is the content address. If you are interested, you can download it.


From the directory, the Code content is very simple. Picture.gocontains all the upload codes. list.html and upload.html contain the template files used, while the uploads directory stores all uploaded image files.


First, let's take a 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 {images = 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 website mainly has three webpages. One is to display the index of all images, the other is to display images, and the other is to upload images to the page.


What are the content of upload.html?

<!doctype html>

A friend who has experience in front-end development can see it at a Glance. This is actually a simple login upload page. What is list.html?

<!doctype html> 

The preceding webpage is not so much a webpage as a template. Because all the images content must be transmitted from the outside world, and all the content will constitute a real webpage.


The above website is simple and clear. If you are interested, you can take a good look.





How can I set imagepicker to Chinese?

1. localized Chinese Target --> Localization native development region: ChinaProject --> Localizations Add a Chinese package. on the Internet, you can also create an empty Chinese (zh-Hans) InfoPlist. strings is fine, so the system will know that the current application is internationalized, and the corresponding interface will be selected based on the current language environment.
 
In android, how do buttons implement multiple languages? Some buttons are ImageButton with words. What should ImageButton do when switching languages?

Drawable-zh-rCN-mdpi creates different drawble folders based on different languages,
Then the image name is the same (Chinese-English), and the system will automatically find the corresponding image resource

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.