Skip to content

Instantly share code, notes, and snippets.

@feyeleanor
Created May 27, 2014 22:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feyeleanor/a1185dd622d1f9fbc6ca to your computer and use it in GitHub Desktop.
Save feyeleanor/a1185dd622d1f9fbc6ca to your computer and use it in GitHub Desktop.
unexpected goroutine behaviour
package main
import (
. "fmt"
. "net/http"
)
const ADDRESS = ":1024"
const SECURE_ADDRESS = ":1025"
func main() {
message := "hello world"
HandleFunc("/hello", func(w ResponseWriter, r *Request) {
w.Header().Set("Content-Type", "text/plain")
Fprintf(w, message)
})
go func() {
ListenAndServe(ADDRESS, nil)
}()
ListenAndServeTLS(SECURE_ADDRESS, "cert.pem", "key.pem", nil)
}
package main
import (
. "fmt"
. "net/http"
)
const ADDRESS = ":1024"
const SECURE_ADDRESS = ":1025"
func main() {
message := "hello world"
HandleFunc("/hello", func(w ResponseWriter, r *Request) {
w.Header().Set("Content-Type", "text/plain")
Fprintf(w, message)
})
go func() {
ListenAndServe(ADDRESS, nil)
}()
go func() {
ListenAndServeTLS(SECURE_ADDRESS, "cert.pem", "key.pem", nil)
}()
for {}
}
@feyeleanor
Copy link
Author

I'd expect the for{} to block and the goroutines to launch successfully, but that doesn't appear to happen on OSX 10.9.3.

@OneOfOne
Copy link

You could use a sync.WaitGroup, but what version of go are you using?:

var wg sync.WaitGroup
wg.Add(2)
go func() {
    ListenAndServe(ADDRESS, nil)
    wg.Done()
}()

go func() {
    ListenAndServeTLS(SECURE_ADDRESS, "cert.pem", "key.pem", nil)
    wg.Done
}()

wg.Wait()

@OneOfOne
Copy link

does runtime.GOMAXPROCS(2) make the 2nd example work for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment