Skip to Content
Learning Functional Programming in Go
book

Learning Functional Programming in Go

by Lex Sheehan
November 2017
Intermediate to advanced
670 pages
17h 35m
English
Packt Publishing
Content preview from Learning Functional Programming in Go

More application code

The first thing we do in the main() function is check the RUN_HTTP_SERVER environment variable. If it's set to true, then the program will set up two routes. The first route /cars returns the index page that displays all the cars that have been loaded from the .csv files. The second route /cars/:id retrieves an individual car object and returns its JSON representation:

func main() {       if os.Getenv("RUN_HTTP_SERVER") == "TRUE" {              router := httprouter.New()              router.GET("/cars", CarsIndexHandler)              router.GET("/cars/:id", CarHandler)              log.Println("Listening on port 8000")              log.Fatal(http.ListenAndServe(":8000", router))

The IndexedCars variable is defined in types.go as follows:

IndexedCar struct {       Index int `json:"index"`       Car ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Functional Programming

Learning Functional Programming

Jack Widman

Publisher Resources

ISBN: 9781787281394Supplemental Content