hello_world_webserver.go 554 B

123456789101112131415161718192021222324
  1. // hello_world_webserver.go
  2. package main
  3. import (
  4. "fmt"
  5. "net/http"
  6. "log"
  7. )
  8. func HelloServer(w http.ResponseWriter, req *http.Request) {
  9. fmt.Println("Inside HelloServer handler")
  10. //fmt.Fprint(w, "Hello, " + req.URL.Path[1:])
  11. fmt.Fprintf(w, "Hello, %s ", req.URL.Path[1:])
  12. // io.WriteString(w, "hello, world!\n")
  13. }
  14. func main() {
  15. http.HandleFunc("/", HelloServer)
  16. err := http.ListenAndServe("localhost:8080", nil)
  17. if err != nil {
  18. log.Fatal("ListenAndServe: ", err.Error())
  19. }
  20. // http.ListenAndServe(":8080", http.HandlerFunc(HelloServer))
  21. }