Standard library / net/http
http.Server.ListenAndServe()
Listens on the configured TCP address and serves HTTP. This example verifies that a closed server returns ErrServerClosed without binding a port. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func (srv *Server) ListenAndServe() errorReturn Value
error
Operation result; check errors and validity indicators when returned.
Working Example
Example Snippetgo
package main
import ( "errors" "fmt" "net/http")
func main() { s := &http.Server{Addr: "127.0.0.1:0"} check(s.Close()) fmt.Println(errors.Is(s.ListenAndServe(), http.ErrServerClosed))}
func must[T any](v T, e error) T { if e != nil { panic(e) } return v}func check(e error) { if e != nil { panic(e) }}