Listener prevents logging and input from stdin

If I comment out lines 19 and 20 (i.e. I don’t start the listener), then the logging and the wait both work. Leaving them in, once the listener starts, no further logging takes place. “before listener starts” gets properly logged, but “after listener starts” does not. Also, wait for input from stdin doesn’t work either. For testing purposes, I’d like to log various messages and wait for input now and again. Any ideas, anyone?

package main
import (“fmt”
“net/http”
“log”
“os”
“bufio”)
var mux = http.NewServeMux()
func index(w http.ResponseWriter, r *http.Request) { }
func main() {
mux.HandleFunc("/", index)
logFile, err1 := os.OpenFile(“logfile.txt”, os.O_RDWR|os.O_CREATE, 0755)
if err1 != nil { fmt.Println(“error opening log file”)
return }
defer logFile.Close()
log.SetOutput(logFile)
fmt.Println(“test log pgm”)
log.Println(“before listener starts”)
err := http.ListenAndServe(":8080", mux) // take this out, and logging works correctly
log.Fatal(err) // and this
log.Println(“after listener starts”)
wait()
return
}
func wait() {
fmt.Println(“waiting…”)
log.Println(“waiting…”)
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString(’\n’)
input=input+“dummy”
}

http.ListenAndServe() blocks, so nothing else will ever be executed until it returns with an error.

Yes, thanks, but I need to have a listener for my application. So I can’t
do logging when I have a listener active? Perhaps there’s another way of
logging. If I just use fmt to write to stdout, there is a limit to how
much output it will store before overwriting earlier stuff with the more
recent stuff. Writing to a file from many different parts of my application
seems like overkill.

Well, I usually log from the handler as soon as a request comes in.

If you really want to log after it, then use a go routine, but beware!

go http.ListenAndServe(…)
fmt.Println("after ListenAndServe")

Here the actual printing my occur before the go routine got some time by the scheduler…