Go 學習筆記15.http服務

http-server.go

package main

import (
	"fmt"
	"io"
	"net/http"
	"strings"
)

// 處理函數
func helloserver(w http.ResponseWriter,r *http.Request) {
	fmt.Println(r.URL.Path)
	strs :=strings.Split(r.URL.Path,"/")
	fmt.Println(strs)
	io.WriteString(w,strs[1]+ "," +strs[2])

}

func main() {
	// 1.設置路由,所有 /hello/* 都回去helloserver函數執行
	http.HandleFunc("/hello/",helloserver)

	// 2.啓動服務
	http.ListenAndServe(":8080",nil)
}

訪問

http://localhost:8080/hello/xiaoming
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章