Hello, Web
The following application serves as a static file at the location /static
, and a dynamic response
at the location /dynamic
:
package main import ( "fmt" "net/http" "time" ) const ( Port = ":8080" ) func serveDynamic(w http.ResponseWriter, r *http.Request) { response := "The time is now " + time.Now().String() fmt.Fprintln(w,response) }
Just as fmt.Println
will produce desired content at the console level, Fprintln
allows you to direct output to any writer. We'll talk a bit more about the writers in Chapter 2, Serving and Routing, but they represent a fundamental, flexible interface that is utilized in many Go applications, not just for the Web:
func serveStatic(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, "static.html") ...
Get Go: Building Web Applications now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.