I have a simple app that I expect to be a daemon that waits for connections, but it looks like it doesn't listen and I even can't find it in the process list.
Code:
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
fmt.Println("Start Server")
log.Print("Start Server")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, World! from linux")
})
log.Print("Going to listen")
if err := http.ListenAndServe(":80", nil); err != err {
log.Print("getting error")
log.Fatal(err)
}
fmt.Println("End Server")
log.Print("End Server")
}
Compile and run:
xxx@xxx:~/go_test$ CGO_ENABLED=0 go build -gcflags "all=-N -l" -o ./main
umen@umen:~/go_test$ ./main
Start Server
2023/11/15 07:40:22 Start Server
2023/11/15 07:40:22 Going to listen
End Server
2023/11/15 07:40:22 End Server
Trying to connect:
xxx@xxx:~$ curl http://localhost
curl: (7) Failed to connect to localhost port 80 after 0 ms: Connection refused
What am I missing here?
Removed the Fatal, still it start and stop without listening to get request.
log.Fatal("Going to listen")
terminates your application execution.
References: