package main
import (
"net"
"net/http"
"time"
)
type HTTP struct {
listener net.Listener
}
func (h *HTTP) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello"))
func main() {
l, _ := net.Listen("tcp", ":3000")
h := new(HTTP)
h.listener = l
go func() {
http.Serve(h.listener, h)
}()
time.Sleep(time.Second * 10)
h.listener.Close()
select{}
這裡有一種方案供你參考。
package main
import (
"net"
"net/http"
"time"
)
type HTTP struct {
listener net.Listener
}
func (h *HTTP) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello"))
}
func main() {
l, _ := net.Listen("tcp", ":3000")
h := new(HTTP)
h.listener = l
go func() {
http.Serve(h.listener, h)
}()
time.Sleep(time.Second * 10)
h.listener.Close()
select{}
}
這裡有一種方案供你參考。