Go Guide, Web Server, Accesshttp://localhost:4000/can’t access the analysis of this website
1. Refer to the Go guide, web server, write file: 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. Accesshttp://localhost:4000/When prompted: The website cannot be accessed, as shown in Figure 1
3. Run in PowerShell: go run web-servers.go, visit againhttp://localhost:4000/When, the output: hello!, as shown in Figure 2
PS E:\wwwroot\go\methods> go run web-servers.go

