創(chuàng)建一個(gè)http服務(wù)宣鄙,大致需要經(jīng)歷兩個(gè)過程炊苫,首先需要注冊路由医咨,即提供url模式和handler函數(shù)的映射搪搏,其次就是實(shí)例化一個(gè)server對象,并開啟對客戶端的監(jiān)聽
server1.go
package main
import (
????????"io"
????????"net/http"
????????"log"
)
func main () {
? ? ? ?// 設(shè)置路由
? ???? http.HandleFunc("/", sayOne)
? ? ? ?// 路由注冊完递览,開始運(yùn)行
? ???? err := http.ListenAndServe(":6055", nil)
? ? ? ?if err != nil {
????????????log.Fatal(err)
? ? ? ?}????
}
func sayOne (w http.ResponseWriter, r *http.Request) {
????????io.WriteString(w, "this is version 1")
}
go run server1.go