Standard library / net/http

http.ListenAndServe()

Starts an HTTP server using the supplied address and handler. Always returns an error; this example tests invalid address rejection, not successful binding. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func ListenAndServe(addr string, handler Handler) error

Parameters & Attributes

NameTypeDescription
addrstringAddress for reverse lookup.
handlerHandlerHandler to invoke for matching requests.

Return Value

error

Operation result; check errors and validity indicators when returned.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net/http")
func main() {  err := http.ListenAndServe("invalid-address", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))  fmt.Println(err != nil)}
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)  }}
Go Reference