使用Golang搭建web服務的實現(xiàn)步驟
如何用golang搭建一個web服務呢?菜鳥官網(wǎng)的go web編程教程已經(jīng)介紹了web服務器的工作原理,這里就不贅述了。
我們先看個例子:http.go
package main
import (
"fmt"
"io"
"log"
"net/http"
)
func main() {
http.HandleFunc("/test", doRequest) // 設置訪問路由
err := http.ListenAndServe(":8000", nil) //設置監(jiān)聽的端口
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func doRequest(w http.ResponseWriter, r *http.Request) {
r.ParseForm() //解析url傳遞的參數(shù),對于POST則解析響應包的主體(request body)
//fmt.Println(r.Form) //這些信息是輸出到服務器端的打印信息
//fmt.Println("path", r.URL.Path)
//fmt.Println("scheme", r.URL.Scheme)
//for k, v := range r.Form {
// fmt.Println("key:", k)
// fmt.Println("value:", strings.Join(v, ""))
//}
fmt.Fprintf(w, "service start...") //這個寫入到w的是輸出到客戶端的 也可以用下面的 io.WriteString對象
//注意:如果沒有調(diào)用ParseForm方法,下面無法獲取表單的數(shù)據(jù)
//query := r.URL.Query()
var uid string // 初始化定義變量
if r.Method == "GET" {
uid = r.FormValue("uid")
} else if r.Method == "POST" {
uid = r.PostFormValue("uid")
}
io.WriteString(w, "uid = "+uid)
}go run http.go命令運行程序。
之后在瀏覽器中輸入地址:http://127.0.0.1:8000/test?uid=10086,看下結(jié)果。

在main函數(shù)中,我們從net/http包中調(diào)用了一個http.HandleFucn函數(shù)來注冊一個處理函數(shù)
這個函數(shù)接受兩個參數(shù)。第一個是字符串,這個就是進行路由匹配,我這里是/test路由。第二個參數(shù)是一個func (ResponseWriter, Request)的簽名。
我們的doRequest函數(shù)就是這樣的簽名。下一行中的http.ListenAndServe(":8000", nil),表示監(jiān)聽localhost的8000端口,暫時忽略掉nil。
在doRequest函數(shù)中我們有兩個參數(shù),一個是http.ResponseWriter類型的。它類似響應流,實際上是一個接口類型。
第二個是http.Request類型,類似于HTTP 請求。我們不必使用所有的參數(shù),如果只是簡單的輸出,那么我們只需要使用http.ResponseWriter,io.WriteString,將會把輸出流寫入數(shù)據(jù)。
我們再稍微改下,大家請注意修改的部分(這里我們只調(diào)整 main函數(shù)部分代碼)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/test", doRequest)
err := http.ListenAndServe(":8000", mux) //設置監(jiān)聽的端口
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}這個例子中,我們不再在函數(shù)http.ListenAndServe使用nil了。這個例子跟上面的例子其實是一樣的。使用http注冊hanlder 函數(shù)模式就是用的ServeMux。
我們再調(diào)整下看下更復雜的例子:
package main
import (
"fmt"
"io"
"log"
"net/http"
)
var mux map[string]func(http.ResponseWriter, *http.Request)
func main() {
server := http.Server{
Addr: ":8000",
Handler: &doHandler{},
}
mux = make(map[string]func(http.ResponseWriter, *http.Request))
mux["/test"] = doRequest
err := server.ListenAndServe()
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
type doHandler struct{}
func (*doHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if res, ok := mux[r.URL.String()]; ok {
res(w, r)
return
}
io.WriteString(w, "url params: "+r.URL.String())
}
func doRequest(w http.ResponseWriter, r *http.Request) {
r.ParseForm() //解析url傳遞的參數(shù),對于POST則解析響應包的主體(request body)
fmt.Fprintf(w, "service start...") //這個寫入到w的是輸出到客戶端的 也可以用下面的 io.WriteString對象
}
這個例子我們沒有定義ServeMux,而是使用了http.Server。都是用net/http包運行了服務器。
到此這篇關于使用Golang搭建web服務的實現(xiàn)步驟的文章就介紹到這了,更多相關Golang搭建web服務內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Go語言規(guī)范context?類型的key用法示例解析
這篇文章主要為大家介紹了Go語言規(guī)范context?類型的key用法示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08
并發(fā)安全本地化存儲go-cache讀寫鎖實現(xiàn)多協(xié)程并發(fā)訪問
這篇文章主要介紹了并發(fā)安全本地化存儲go-cache讀寫鎖實現(xiàn)多協(xié)程并發(fā)訪問,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
Go語言中slice作為參數(shù)傳遞時遇到的一些“坑”
這篇文章主要給大家介紹了關于Go語言中slice作為參數(shù)傳遞時遇到的一些“坑”,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-03-03

