Created
May 27, 2014 22:47
-
-
Save feyeleanor/a1185dd622d1f9fbc6ca to your computer and use it in GitHub Desktop.
unexpected goroutine behaviour
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} | |
} |
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()
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
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.