GO語言"net/http"包中"ListenAndServe"函數的用法及代碼示例。
用法:
func ListenAndServe(addr string, handler Handler) error
ListenAndServe 偵聽 TCP 網絡地址 addr,然後使用處理程序調用 Serve 以處理傳入連接上的請求。接受的連接配置為啟用 TCP keep-alives。
處理程序通常為零,在這種情況下使用DefaultServeMux。
ListenAndServe 始終返回非零錯誤。
例子:
package main
import (
"io"
"log"
"net/http"
)
func main() {
// Hello world, the web server
helloHandler := func(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "Hello, world!\n")
}
http.HandleFunc("/hello", helloHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
相關用法
- GO ListenAndServeTLS用法及代碼示例
- GO Listener用法及代碼示例
- GO LimitReader用法及代碼示例
- GO LeadingZeros32用法及代碼示例
- GO Logger.Output用法及代碼示例
- GO LastIndex用法及代碼示例
- GO Log10用法及代碼示例
- GO Log用法及代碼示例
- GO LeadingZeros8用法及代碼示例
- GO LeadingZeros16用法及代碼示例
- GO LastIndexAny用法及代碼示例
- GO Logger用法及代碼示例
- GO Len8用法及代碼示例
- GO LoadLocation用法及代碼示例
- GO LastIndexFunc用法及代碼示例
- GO Len64用法及代碼示例
- GO LoadX509KeyPair用法及代碼示例
- GO LookPath用法及代碼示例
- GO LookupEnv用法及代碼示例
- GO Len32用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 ListenAndServe。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。