使用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對象
//注意:如果沒有調用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,看下結果。

在main函數(shù)中,我們從net/http包中調用了一個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ù)。
我們再稍微改下,大家請注意修改的部分(這里我們只調整 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。
我們再調整下看下更復雜的例子:
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服務內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
CentOS 32 bit安裝golang 1.7的步驟詳解
Go是Google開發(fā)的一種編譯型,并發(fā)型,并具有垃圾回收功能的編程語言。在發(fā)布了6個rc版本之后,Go 1.7終于正式發(fā)布了。本文主要介紹了在CentOS 32 bit安裝golang 1.7的步驟,文中給出了詳細的步驟,相信對大家的學習和理解具有一定的參考借鑒價值,下面來一起看看吧。2016-12-12
Golang實現(xiàn)自己的Redis(TCP篇)實例探究
這篇文章主要介紹了Golang實現(xiàn)自己的Redis(TCP篇)實例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01
Golang實現(xiàn)四種負載均衡的算法(隨機,輪詢等)
本文介紹了示例介紹了Golang 負載均衡的四種實現(xiàn),主要包括了隨機,輪詢,加權輪詢負載,一致性hash,感興趣的小伙伴們可以參考一下2021-06-06

