The Go Programming Language (Addison-Wesley Professional Computing Series) by Brian W. Kernighan

The Go Programming Language (Addison-Wesley Professional Computing Series) by Brian W. Kernighan

Author:Brian W. Kernighan [Kernighan, Brian W.]
Language: eng
Format: epub
Tags: Computers & Technology, Programming Languages, Reference, Programming
ISBN: 9780134190440
Amazon: B0184N7WWS
Publisher: Addison-Wesley Professional
Published: 2015-11-15T23:00:00+00:00


It’s easy to see from the code above how one would construct a program in which there are two different web servers, listening on different ports, defining different URLs, and dispatching to different handlers. We would just construct another ServeMux and make another call to ListenAndServe, perhaps concurrently. But in most programs, one web server is plenty. Also, it’s typical to define HTTP handlers across many files of an application, and it would be a nuisance if they all had to be explicitly registered with the application’s ServeMux instance.

So, for convenience, net/http provides a global ServeMux instance called DefaultServeMux and package-level functions called http.Handle and http.HandleFunc. To use DefaultServeMux as the server’s main handler, we needn’t pass it to ListenAndServe; nil will do.

The server’s main function can then be simplified to

Click here to view code image

gopl.io/ch7/http4

func main() { db := database{"shoes": 50, "socks": 5} http.HandleFunc("/list", db.list) http.HandleFunc("/price", db.price) log.Fatal(http.ListenAndServe("localhost:8000", nil)) }



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.