Can I setup multi port from one web app with Go?

No, you cannot.

You can however start multiple listeners on different ports

go http.ListenAndServe(PORT, handlerA)
http.ListenAndServe(PORT, handlerB)

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)
}

Tags:

Go