FAQ
I want to be able to run two separate http servers concurrently on
different port numbers. http.ListenAndServe already puts the script into a
never ending state, but if I start two separate go routines, the script
will immediately exit. I've also tried only calling one as a go routine but
I get an error of 'multiple registrations'

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Nguyên Nguyễn Văn Cao at Jun 11, 2013 at 2:43 am
    Well, how about this http://play.golang.org/p/L6KVUuApzt

    Vào 07:34:58 UTC+7 Thứ ba, ngày 11 tháng sáu năm 2013, kyle.a...@gmail.com
    đã viết:
    I want to be able to run two separate http servers concurrently on
    different port numbers. http.ListenAndServe already puts the script into a
    never ending state, but if I start two separate go routines, the script
    will immediately exit. I've also tried only calling one as a go routine but
    I get an error of 'multiple registrations'
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Jesse McNelis at Jun 11, 2013 at 2:44 am

    On Tue, Jun 11, 2013 at 10:34 AM, wrote:
    I want to be able to run two separate http servers concurrently on different
    port numbers. http.ListenAndServe already puts the script into a never
    ending state, but if I start two separate go routines, the script will
    immediately exit. I've also tried only calling one as a go routine but I get
    an error of 'multiple registrations'
    An example of your code would help us help you.

    The 'multiple registrations' error is the result of adding multiple
    handlers for the same url to a servemux.
    http://golang.org/src/pkg/net/http/server.go?#L1432

      http.ListenAndServe() expects an address to listen on and a
    http.Handler to handle the incoming requests.
    "Handler is typically nil, in which case the DefaultServeMux is used."
    If you're starting two http.ListenAndServe() and they both have a nil
    Handler than you're sharing the DefaultServeMux between
    them and routes added to one will be available on the other.




    --
    =====================
    http://jessta.id.au

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Naitik Shah at Jun 11, 2013 at 2:53 am
    One in a new goroutine and one in the main goroutine?
    On Mon, Jun 10, 2013 at 7:43 PM, Jesse McNelis wrote:
    On Tue, Jun 11, 2013 at 10:34 AM, wrote:
    I want to be able to run two separate http servers concurrently on different
    port numbers. http.ListenAndServe already puts the script into a never
    ending state, but if I start two separate go routines, the script will
    immediately exit. I've also tried only calling one as a go routine but I get
    an error of 'multiple registrations'
    An example of your code would help us help you.

    The 'multiple registrations' error is the result of adding multiple
    handlers for the same url to a servemux.
    http://golang.org/src/pkg/net/http/server.go?#L1432

    http.ListenAndServe() expects an address to listen on and a
    http.Handler to handle the incoming requests.
    "Handler is typically nil, in which case the DefaultServeMux is used."
    If you're starting two http.ListenAndServe() and they both have a nil
    Handler than you're sharing the DefaultServeMux between
    them and routes added to one will be available on the other.




    --
    =====================
    http://jessta.id.au

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.


    --
    -Naitik

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Alexandre Fiori at Jun 11, 2013 at 5:45 am
    I've been using this:

    http://play.golang.org/p/ZPSZVJeBPd

    On Monday, June 10, 2013 10:53:13 PM UTC-4, Naitik Shah wrote:

    One in a new goroutine and one in the main goroutine?
    On Mon, Jun 10, 2013 at 7:43 PM, Jesse McNelis wrote:
    On Tue, Jun 11, 2013 at 10:34 AM, <kyle.a...@gmail.com <javascript:>>
    wrote:
    I want to be able to run two separate http servers concurrently on
    different
    port numbers. http.ListenAndServe already puts the script into a never
    ending state, but if I start two separate go routines, the script will
    immediately exit. I've also tried only calling one as a go routine but
    I get
    an error of 'multiple registrations'
    An example of your code would help us help you.

    The 'multiple registrations' error is the result of adding multiple
    handlers for the same url to a servemux.
    http://golang.org/src/pkg/net/http/server.go?#L1432

    http.ListenAndServe() expects an address to listen on and a
    http.Handler to handle the incoming requests.
    "Handler is typically nil, in which case the DefaultServeMux is used."
    If you're starting two http.ListenAndServe() and they both have a nil
    Handler than you're sharing the DefaultServeMux between
    them and routes added to one will be available on the other.




    --
    =====================
    http://jessta.id.au

    --
    You received this message because you are subscribed to the Google
    Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send
    an email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.


    --
    -Naitik
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Rodrigo Kochenburger at Jun 11, 2013 at 8:24 am
    You can also manually instantiate http.Server instances and run each's
    Server.ListenAndServe() in separate goroutines. It's more work but it gives
    you more freedom since you can have completely independent executions
    without any shared state, even completely separated ServeMux.

    The downside is that any package that register things on DefaultServeMux
    won't work (i.e. net/http/pprof).
    On Monday, June 10, 2013 5:34:58 PM UTC-7, kyle.a...@gmail.com wrote:

    I want to be able to run two separate http servers concurrently on
    different port numbers. http.ListenAndServe already puts the script into a
    never ending state, but if I start two separate go routines, the script
    will immediately exit. I've also tried only calling one as a go routine but
    I get an error of 'multiple registrations'
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Kmoving at Jan 23, 2015 at 1:29 pm
    I have the similar needs and google brings me here, after some dig on the
    net/http/server.go code,
    i think the better way to do this maybe like this:

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

    hope this will be helpful to you:)




    在 2013年6月11日星期二 UTC+8上午8:34:58,Kyle Wolfe写道:
    I want to be able to run two separate http servers concurrently on
    different port numbers. http.ListenAndServe already puts the script into a
    never ending state, but if I start two separate go routines, the script
    will immediately exit. I've also tried only calling one as a go routine but
    I get an error of 'multiple registrations'
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Donatj at Feb 25, 2015 at 9:35 pm
    This is very helpful to me and exactly what I was looking for, thank you!
    On Friday, January 23, 2015 at 5:45:54 AM UTC-6, kmo...@gmail.com wrote:

    I have the similar needs and google brings me here, after some dig on the
    net/http/server.go code,
    i think the better way to do this maybe like this:

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

    hope this will be helpful to you:)




    在 2013年6月11日星期二 UTC+8上午8:34:58,Kyle Wolfe写道:
    I want to be able to run two separate http servers concurrently on
    different port numbers. http.ListenAndServe already puts the script into a
    never ending state, but if I start two separate go routines, the script
    will immediately exit. I've also tried only calling one as a go routine but
    I get an error of 'multiple registrations'
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 11, '13 at 2:24a
activeFeb 25, '15 at 9:35p
posts8
users8
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase