In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to write Web applications in Go language. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Start here.
If you want to have a computer or virtual machine that can run Go language, how to install Go, please refer to the installation Go tutorial. First create a directory, create a wiki.go file under the directory, open it with your favorite editor and enter the following:
Package main import ("fmt", "io/ioutil", "os")
Both fmt,ioutil and os are standard libraries for the go language. I'll add other methods and more packages later.
Data structure
Let's declare a data structure that mainly consists of two fields, one is the title and the other is the content.
Type Page struct {Title string Body [] byte}
Next, we write a save method for the structure Page, with the following code:
Func (p * Page) save () os.Error {filename: = p.Title + ".txt" return ioutil.WriteFile (filename, p.Body, 0600)}
The signature of this method is to receive a pointer to the Page structure and return an os.Error error.
Http package and template package are still used in the following code, the specific content is referred to the specific code, and then it will not be posted in detail here. Here are the template contents, put them in the same directory as wiki.go.
Edit Page template eidt.html
Editing {{.Title | html}} {{printf "% s" .body | html}}
View page template view.html
{{.Title | html}}
[edit]
{{printf's ".body | html}}
Complete code: wiki.go
Package main import ("http"io/ioutil"os"regexp"template") type Page struct {Title string Body [] byte} func (p * Page) save () os.Error {filename: = p.Title + ".txt" return ioutil.WriteFile (filename, p.Body, 0600)} func loadPage (title string) (* Page, os.Error) {filename: = title + ".txt" body Err: = ioutil.ReadFile (filename) if err! = nil {return nil, err} return & Page {Title: title, Body: body}, nil} func viewHandler (w http.ResponseWriter, r * http.Request, title string) {p, err: = loadPage (title) if err! = nil {http.Redirect (w, r, "/ edit/" + title, http.StatusFound) return} renderTemplate (w, "view") P)} func editHandler (w http.ResponseWriter, r * http.Request, title string) {p, err: = loadPage (title) if err! = nil {p = & Page {Title: title}} renderTemplate (w, "edit", p)} func saveHandler (w http.ResponseWriter, r * http.Request, title string) {body: = r.FormValue ("body") p: = & Page {Title: title Body: [] byte (body)} err: = p.save () if err! = nil {http.Error (w, err.String (), http.StatusInternalServerError) return} http.Redirect (w, r, "/ view/" + title, http.StatusFound)} var templates = make (map [string] * template.Template) func init () {for _, tmpl: = range [] string {"edit" "view"} {t: = template.Must (template.ParseFile (tmpl + ".html") templates [tmpl] = t}} func renderTemplate (w http.ResponseWriter, tmpl string, p * Page) {err: = templates [tmpl] .Execute (w, p) if err! = nil {http.Error (w, err.String () Http.StatusInternalServerError)} const lenlenPath = len ("/ view/") var titleValidator = regexp.MustCompile ("^ [a-zA-Z0-9] + $") func makeHandler (fn func (http.ResponseWriter, * http.Request, string)) http.HandlerFunc {return func (w http.ResponseWriter, r * http.Request) {title: = r.URL.Path [lenPath:] if! titleValidator.MatchString (title) {http.NotFound (w R) return} fn (w, r, title)} func main () {http.HandleFunc ("/ view/", makeHandler (viewHandler)) http.HandleFunc ("/ edit/", makeHandler (editHandler)) http.HandleFunc ("/ save/", makeHandler (saveHandler)) http.ListenAndServe (": 8080", nil)}
Run the test:
$8g wiki.go
$8l wiki.8
$. / 8.out
Enter the address in the address bar: http://localhost:8080/view/aNewPage
Effect picture:
This is how to write Web applications in Go language shared by Xiaobian. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.