I would like to start an HTTP server in a goroutine but have a its parent return an error if the startup failed. Is there a good way for doing this? I tried passing the error in a channel. But this makes the parent block even if the server started successfully (unless I introduce an arbitrary timeout for listening on that channel).
func Start() error {
go func() {
srv = &http.Server{...}
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
// return err from Start()
}
}
// return nil if server started successfully
}
ListenAndServe
blocks until the server shuts down; by wrapping what is not aStart
function in aStart
function, you're changing the semantics of the call, and you're going to have to account for that somehow.