1、参考 Go 指南中,Web 服务器,编写文件:web-servers.go
package main import ( "fmt" "log" "net/http" ) type Hello struct{} func (h Hello) ServeHTTP( w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "Hello!") } func main() { var h Hello err := http.ListenAndServe("localhost:4000", h) if err != nil { log.Fatal(err) } }
2、访问 http://localhost:4000/ 时,提示:无法访问此网站,如图1
3、在 PowerShell 中运行:go run web-servers.go,再次访问 http://localhost:4000/ 时,输出:Hello!,如图2
PS E:/wwwroot/go/methods> go run web-servers.go
原创文章,作者:kepupublish,如若转载,请注明出处:https://blog.ytso.com/181465.html