This is the 7th day of my participation in the August More Text Challenge

Web application is a kind of application that can be accessed through the Web. The biggest advantage of Web application is that users can easily access the application. Users only need to have a browser and do not need to install other software. Web applications are too common for us in the Internet age. Any language that can develop software that interacts with humans is bound to support Web application development.

This series of articles will introduce Go Web applications and practices. Welcome to follow.

In the previous article, we introduced the Go Web process framework gin, Golang provides a very good NET/HTTP library, the implementation of HTTP protocol is very good, based on this framework, is not difficult, so there are many frameworks in the ecosystem. Gin, Gorilla/MUx, Echo, etc.

This article covers server-side storage. The Web server needs to persist some of the client’s data, including files, memory, and databases. We will introduce the memory-based storage implementation below.

Memory storage

The first is memory-based storage. Data structures such as arrays, slicing, maps, and structures can be used to store relevant data information in memory in Go.

We build on the previous login example by storing the login user information in memory and adding an interface that returns the user information for the specified user name. The code is as follows:

package main import ( "fmt" "html/template" "log" "net/http" ) type User struct { Id int Name string Password string } var UserById = make(map[int]*User) var UserByName = make(map[string][]*User) func loginMemory(w http.ResponseWriter, R * http.request) {fmt.Println("method:", r.thod) if r.thod == "GET" {t, _ := template.parseFiles ("login.tpl") log.println (t.execute (w, nil))} else { _ = r.parseForm () fmt.println ("username:", r.forest ["username"]) fmt.println ("password:", r.Form["password"]) user1 := User{1, r.Form.Get("username"), r.Form.Get("password")} store(user1) if pwd := r.Form.Get("password"); PWD == "123456" {// Verify password is correct FMT.Fprintf(w, "Welcome to login, Hello %s!" } else {FMT.Fprintf(w, "Password error, please try again!" Func store(user user) {// Store user information UserById[user.id] = &user // Store UserByName[user.name] = by Id Append (UserByName[user.name], &user) // Store by user Name} func userInfo(w http.responsewriter, r *http.Request) { fmt.Println(UserById[1]) r.ParseForm() for _, User := range UserByName[r.form.get ("username")] {FMT.Fprintf(w," %v",user)}} func main() {range UserByName[r.form.get ("username")] {FMT. Http.handlefunc ("/login", loginMemory) // Set the route to access http.handlefunc ("/info", UserInfo) // Access user information err := http.ListenAndServe(":8080", nil) // Set the listening port if err! = nil { log.Fatal("ListenAndServe: ", err) } }Copy the code

The login result is the same as before, then we access the /info interface and get the following result:

As you can see, the content returned is the information we entered when we logged in. Looking at the code implementation above, we define global variables at the beginning of the program:

var UserById = make(map[int]*User)
var UserByName = make(map[string][]*User)
Copy the code

UserById will map the unique Id of a post to a pointer to the post, while UserByName will map the User Name to a slice that can contain multiple Pointers to the User. These two variable mappings point to Pointers to User, so you ensure that whichever way you retrieve User information, you get the same User, not different copies of the same User. The store method is used to store user information. When obtaining user information, the system returns the user information based on the specified user name.

summary

This article mainly introduces the Go server based on memory storage, relatively simple. Unlike other languages, Go does not have a built-in driver to support any database, but it does have a database/ SQL interface that users can use to develop drivers for their databases. One advantage of this is that as long as the code is developed to a standard interface, no changes are required when the database needs to be migrated later. So what are the standard interfaces defined by Go? The next article will give a detailed introduction.

Read the latest article, pay attention to the public number: AOHO Qiusuo