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.