Input for Go processing form

Source: Internet
Author: User
Package Main
Import (
"FMT"
"Html/template"
"Log"
"Net/http"
"Strings"
)
Func Main () {
http. Handlefunc ("/", SayHello)   //Set Access routing
http. Handlefunc ("/login", login)//Set Access routing
ERR: = http. Listenandserve (": 9999", nil)//Set the listening address and port
If err! = Nil {
Log. Fatal ("Listenandserve:", err)
}
}
Func SayHello (w http. Responsewriter, R *http. Request) {//For Output first page
R.parseform ()
Fmt. Println (R.form)
Fmt. PRINTLN ("Path", R.url. Path)
Fmt. PRINTLN ("scheme", R.url. Scheme)
For k, V: = Range R.form {
Fmt. Println ("Key:", K)
Fmt. Println ("Value:", strings. Join (V, ""))
}
Fmt. fprintf (W, "Hello Golang")
}
/*
The function performs different actions depending on the submission method.
If the commit method is get, output the login page to the client
Gets the form content submitted by the client if the submission method is post
*/
Func Login (w http. Responsewriter, R *http. Request) {
Fmt. Println ("Method:", R.method)
if R.method = = "GET" {
T, _: = template. Parsefiles ("login.html")
T.execute (W, Nil)
} else {
R.parseform ()//The Parseform method call here is required, otherwise the form contents cannot be obtained below
The following code is a logical processing of the contents of a form
Fmt. Println ("Username:", r.form["username"])
Fmt. Println ("Password:", r.form["password"])
}
}
The console runs directly:
Access the first page using IE:
Console output:
Next, use IE to access the login page:
Console output:
Enter the user name and password, click the login button, submit the data to the background for logical processing:
Console output:
The user name and password in the form are printed in the background.

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.