Is it right when I use the method "mux.HandleFunc()"

166 Views Asked by At

I'm a green hand with Go. I have a problem with this code.`

package main

import (
   "fmt"
   "log"
   "net/http"
)

func main() {
   mux := http.NewServeMux()
   mux.HandleFunc("/", homePage)
   mux.HandleFunc("/ws", wsEndpoint)
   if err := http.ListenAndServe(":8080", mux); err != nil {
      log.Fatalf("fatal error!")
   }
}

func homePage(w http.ResponseWriter, r *http.Request) {
   fmt.Println("Home Page")
   fmt.Fprint(w, "Home Page")
}

func wsEndpoint(w http.ResponseWriter, r *http.Request) {
   fmt.Println("hello world")
   fmt.Fprint(w, "hello world")
}

` when I curl the url "http://localhost:8080/ws", the goland console prints "Home Page". I cannot understand it. Any help, thanks. I use go version of 1.17.8

I have no idea why it excutes "fmt.Println("Home Page")" Thank you for any help.

0

There are 0 best solutions below