golang web服务器
The Go programming language is well-known for having a built-in web server. In this article you will learn how you can easily make your own web server with Go. You won’t need any other packages beside the ones that are already built in!
Go编程语言因具有内置的Web服务器而闻名。 在本文中,您将学习如何使用Go轻松制作自己的Web服务器。 除了已经内置的软件包之外,您不需要任何其他软件包!
webserver.go
webserver.go
package main
import (
"net/http"
"io"
)
func main() {
http.HandleFunc("/", servePage)
http.ListenAndServe(":8080", nil)
}
func servePage(writer http.ResponseWriter, reqest *http.Request) {
io.WriteString(writer, "Hello world!")
}
net/httpio
net/httpio
mainservePage/URL
mainservePage/URL
ListenAndServeportstring8080handlerHandlernil
ListenAndServeportstring8080handlerHandlernil
servePageioWriteStringHello world!
servePageioWriteStringHello world!Hello world!
servePagewriterrequestHTTPrequest
servePagewriterrequestHTTPrequest
go run webserver.gohttp://localhost:8080
go run webserver.gohttp://localhost:8080
golang web服务器