This is the first day of my participation in Gwen Challenge
Tip: This series is for readers with a persistent urge to Go
Discussion on Golang Web Service
Golang Web development is an important and competitive application, and this summary will look at how to create a simple Web service in Golang.
In cases where a Web framework is not applicable, you can build a Web service using net/ HTTP packages.
- Here we use
net/http
Create a Web service that prints the request URL.
package main
import (
//"log"
"fmt"
"net/http"
)
func main(a) {
http.HandleFunc("/", handler)
http.ListenAndServe("localhost:6677".nil)}func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "url.path=%q\n", r.URL.Path) // Output to the file stream
}
Copy the code
The http.HandleFunc function can be understood as URL routing.
ListenAndServe is at the heart of web service creation.
Handler is an HTTP Request handler that accepts an HTTP. ResponseWriter file stream and an object of type http.Request.
[root@VM-0-5-centos ~]# curl localhost:6677/123
url.path="/123"
Copy the code
- We use the handler function to calculate the number of visits to the URL.
Introduce mutexes in Golang Sync so that only one Goroutine changes the count count if multiple requests exist simultaneously. Learn more about mutex later.
package main
import (
//"log"
"fmt"
"net/http"
"sync"
)
var count int
var mutex sync.Mutex // Use mutex
func main(a) {
http.HandleFunc("/", handler)
http.ListenAndServe("localhost:6677".nil)}func handler(w http.ResponseWriter, r *http.Request) {
mutex.Lock()
count++
mutex.Unlock()
fmt.Fprintf(w, "request url.path:%q has %d times\n", r.URL.Path, count)
}
Copy the code
Let’s look at the request result as follows:
[root@VM-0-5-centos ~]# curl localhost:6677/golang
request url.path:"/golang" has 1 times
[root@VM-0-5-centos ~]# curl localhost:6677/golang
request url.path:"/golang" has 2 times
[root@VM-0-5-centos ~]# curl localhost:6677/golang
request url.path:"/golang" has 3 times
Copy the code
http.Request
Type objects in addition toURL.Path
In addition to the propertyMethod
,Proto
And so on. We’re throughhandler
The functions are printed separately.
package main
import (
//"log"
"fmt"
"net/http"
"sync"
)
var count int
var mutex sync.Mutex // Use mutex
func main(a) {
http.HandleFunc("/", handler)
http.ListenAndServe("localhost:6677".nil)}func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s,%s,%s,\n", r.Method, r.URL, r.Proto)
fmt.Fprintf(w, "host:%q\nremoteaddr:%q\n", r.Host, r.RemoteAddr)
for k, v := range r.Header {
fmt.Fprintf(w, "Header[%q]:%q\n", k, v)
}
for k, v := range r.Form {
fmt.Fprintf(w, "Form[%q]:%q\n", k, v)
}
}
Copy the code
Create the form and accept it with the following output:
/ / the output of the GET/helloweb, HTTP / 1.1, Host: "localhost: 6677" remoteaddr: "127.0.0.1:58088" Header [" the user-agent "] : [" curl / 7.29.0 "] Header [" Accept "] : [" * / * "] Form[parm1]:hello Form[parm2]:webCopy the code
This is the end of a brief introduction to Golang Web services. Next, I will study the wonderful details and essence of Golang in depth.
Feel free to point out any shortcomings in the comments section.
Favorites, likes and questions are welcome. Follow the top water cooler managers and do something other than pipe hot water.