The Go language Implementation processes form input _golang

Source: Internet
Author: User

Login.html

Copy Code code as follows:

<body>
<form action= "Http://localhost:9090/login" method= "POST" >
User name: <input type= "text" name= "username" >
Password: <input type= "text" name= "password" >
<input type= "Submit" value= "Login" >
</form>
</body>

Main.go

Copy Code code as follows:

Package Main
Import (
"FMT"
"Html/template"
"Log"
"Net/http"
"Strings"
)
Func sayhelloname (w http. Responsewriter, R *http. Request) {
Resolving parameters passed by URL
R.parseform ()
Printing information on the service side
Fmt. Println (R.form)
Fmt. PRINTLN ("Path", R.url.) Path)
Fmt. Println ("Scheme"), R.url. Scheme)
Fmt. Println (r.form["Url_long"])
For k, V: = Range R.form {
Fmt. Println ("Key:", K)
The join () method is used to put all elements in an array into a string.
element is delimited by the specified delimiter.
Fmt. Println ("Val:", Strings.) Join (V, ""))
}
Output to Client
Fmt. fprintf (w, "Hello astaxie!")
}
Func Login (w http. Responsewriter, R *http. Request) {
Fmt. Println ("Method:", R.method)
if R.method = = "Get" {
T, _: = template. Parsefiles ("login.html")
To perform a parse template
Func (t *template) Execute (WR io. Writer, Data interface{}) error {
T.execute (W, Nil)
} else {
R.parseform ()
Fmt. Println ("Username:", r.form["username"])
Fmt. Println ("Password:", r.form["password"])
}
}
Func Main () {
Setting access routes
http. Handlefunc ("/", Sayhelloname)
http. Handlefunc ("/login", login)
Setting Up listening ports
ERR: = http. Listenandserve (": 9090", nil)
If Err!= nil {
Log. Fatal ("Listenandserve:", err)
}
}

The above mentioned is the entire content of this article, I hope you can enjoy.

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.