As I know, I can run simple web server with Golang just use http
package, like
http.ListenAndServe(PORT, nil)
where PORT is TCP address to listen.
Can I use PORT as PORTS, for example http.ListenAndServe(":80, :8080", nil)
from one application?
Possible my question is stupid, but "Who don't ask, He will not get answer!"
Here is a simple working Example:
package main
import (
"fmt"
"net/http"
)
func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello")
}
func world(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "world")
}
func main() {
serverMuxA := http.NewServeMux()
serverMuxA.HandleFunc("/hello", hello)
serverMuxB := http.NewServeMux()
serverMuxB.HandleFunc("/world", world)
go func() {
http.ListenAndServe("localhost:8081", serverMuxA)
}()
http.ListenAndServe("localhost:8082", serverMuxB)
}
No, you cannot.
You can however start multiple listeners on different ports
go http.ListenAndServe(PORT, handlerA)
http.ListenAndServe(PORT, handlerB)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With