Go编程基础4-Http服务器

1、http服务器

package main
import(
    "net/http"
    "log"
)
func main(){//注册某个函数专门响应某个路由"/",函数签名符合
    http.HandleFunc("/",func(w http.ResponseWriter,r *http.Request){
        w.Write([]byte("Hello,this is version 1!"))
    })
    http.HandleFunc("/bye",sayBye)
    log.Println("Starting server ... v1")
    log.Fatal(http.ListenAndServe(":4000",nil))
}
func sayBye( w http.ResponseWriter,r *http.Request){
    w.Write([]byte("Bye bye,this is version 1!"))
}
go run test.go
2018/07/09 14:32:08 Starting server ... v1

浏览器打开访问:http://localhost:4000/
Go编程基础4-Http服务器
浏览器打开访问:http://localhost:4000/bye
Go编程基础4-Http服务器

猜你喜欢

转载自blog.51cto.com/daixuan/2139192