这是我使用的完整解决方案。我在问题中发布的代码是简化版本,目的是清楚地进行演示。
// Create the socket to listen on:l, err := net.Listen(socketType, socketAddr)if err != nil { log.Fatal(err) return}// Unix sockets must be unlink()ed before being reused again.// Handle common process-killing signals so we can gracefully shut down:sigc := make(chan os.Signal, 1)signal.Notify(sigc, os.Interrupt, os.Kill, syscall.SIGTERM)go func(c chan os.Signal) { // Wait for a SIGINT or SIGKILL: sig := <-c log.Printf("Caught signal %s: shutting down.", sig) // Stop listening (and unlink the socket if unix type): l.Close() // And we're done: os.Exit(0)}(sigc)// Start the HTTP server:log.Fatal(http.Serve(l, http.HandlerFunc(indexHtml)))我肯定希望这是一个好而有效的Go代码,它将使Go作者感到骄傲。在我看来确实如此。如果不是这样,那会让我感到尴尬。:)
对于任何好奇的人来说,这都是https://github.com/JamesDunne/go-index-
html的一部分,它是一个简单的HTTP目录列表生成器,具有Web服务器无法为您提供的一些额外功能。



