This article was first published from “MOOC.com”. If you want to know more about IT dry goods and hot news in the programmer circle, please pay attention to “MOOC.com” and “MOOC.com Official Account”!
Author: Codey|MOOC Lecturer
Build a simple login function with Go language
If you have just learned the basic features of the Go language recently, you also have a certain grasp and understanding of the Go language. Then Xiaomu will take you to learn how to use the Go language to build a web service that provides login functions.
1. Build service
It is very easy to build an http service in Go language, just one line of code.
Code sample:
package main import ( "net/http" ) func main() { http.ListenAndServe("127.0.0.1:9300", nil) //Set the listening port }
Run the above code to get a service, enter ??http://127.0.0.1:9300/?
? on the browser, since no route is written, only a 404 prompt will appear:
2. Write routing
The service is ready to run, and the next step is to write a routing interface that can be externally accessed. There are two types of http requests, POST requests and GET requests. The first thing we want to achieve is a route that opens the login page of the website ??/index?
?, and we need to write a route that can respond to GET requests.
Code sample:
package main import ( "net/http" ) func main() { //Set the access route http.HandleFunc("/index", func(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { w.Write([]byte("<h1>Hello Codey!<h1>")) } }) http.ListenAndServe("127.0.0.1:9300", nil) //Set the listening port }
Enter ??127.0.0.1:9300/index?
? in the browser:
Here you can Combined with the idea of functional programming, take out the index processing function as a variable, the modified code is as follows
package main import ( "net/http" ) func main() { http.HandleFunc("/index", index) //Set the access route http.ListenAndServe("127.0.0.1:9300", nil) //Set the listening port } func index(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { w.Write([]byte("<h1>Hello Codey!<h1>")) } }
Then modify the output string to make it output a page. The modified code is as follows
package main import ( "net/http" ) func main() { http.HandleFunc("/index", index) //Set the access route http.ListenAndServe("127.0.0.1:9300", nil) //Set the listening port } func index(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { w.Write([]byte(`<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Go language combat 1</title> </head> <body> <div> <h3>Login</h3> <form> <div> <div> <input type="text" id="username" name="username" placeholder="Please enter account number"> </div> </div> <div> <div> <input type="password" class="form-control" id="password" name="password" placeholder="Please enter the password"> </div> </div> <div> <div> <button id="loginbtn" type="button" >login</button> </div> </div> </form> </div> </body> </html>`)) } }
Run the above code, then enter ??127.0.0.1:9300/index?
? into your browser again.
3. Configure page to html
Generally, writing a web application involves a lot of html files, and it is impossible for us to put all of them in the strings of the Go file, which is inconvenient for debugging and also affects code maintenance. So we usually load the html file directly.
Code sample:
package main import ( "net/http" "text/template" ) func main() { http.HandleFunc("/index", index) //Set the access route http.ListenAndServe("127.0.0.1:9300", nil) //Set the listening port } func index(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { t, _ := template.ParseFiles("view/index.html")//load html file t.Execute(w, nil)//output the file to the browser } }
The directory structure is as follows
The code of index.html is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Go language combat 1</title> </head> <body> <div> <h3>Login</h3> <form> <div> <div> <input type="text" id="username" name="username" placeholder="Please enter account number"> </div> </div> <div> <div> <input type="password" id="password" name="password" placeholder="Please enter the password"> </div> </div> <div> <div> <button id="loginbtn" type="button" >login</button> </div> </div> </form> </div> </body> </html>
To execute the above Go language code, enter ??127.0.0.1:9300/index?
? in the browser.
4. Data transmission
Clicking login on the html page does not respond temporarily. In order to submit the page to the server, we need to write another route to receive data on the server. This route needs to be able to receive POST requests. Then in this route, you need to be able to verify whether the account password is correct, if so, jump to the home page, if not, give a prompt and jump to the login page.
code example
package main import ( "net/http" "text/template" ) func main() { http.HandleFunc("/index", index) //Set the access route http.HandleFunc("/check", check) http.ListenAndServe("127.0.0.1:9300", nil) //Set the listening port } func check(w http.ResponseWriter, r *http.Request) { if r.Method == "POST" { accountID := r.FormValue("username")//Get account password := r.FormValue("password")//Get password if accountID == "Codey" & amp; & amp; password == "12345" { //jump to homepage t, _ := template.ParseFiles("view/home.html") t. Execute(w, nil) } else { //jump to login w.Write([]byte("<script>alert('account or password is incorrect')</script>")) t, _ := template.ParseFiles("view/index.html") t. Execute(w, nil) } } } func index(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { t, _ := template.ParseFiles("view/index.html") t. Execute(w, nil) } }
The code for home.html is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Go language combat 1</title> </head> <body> <div> <h3>Homepage</h3> here is the home page </div> </body> </html>
To execute the above Go language code, enter ??127.0.0.1:9300/index?
? in the browser.
Enter the correct account: Codey, password: 12345
Then click Login and you will be redirected to the home page
If you enter the wrong account password, it will not jump
Then jump back to the login page
A simple login function is built.
Have you learned to use up (hui) yet!
Welcome to pay attention to the official account of “MOOC”. We will always insist on providing high-quality content in the IT circle, sharing dry knowledge, and let’s grow together!
This article was originally published on Muke.com, please indicate the source for reprinting, thank you for your cooperation
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Go skill tree HomepageOverview 3526 people are studying systematically