static.go 486 B

123456789101112131415161718192021222324252627
  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. )
  6. func handler(w http.ResponseWriter, r *http.Request) {
  7. w.Header().Set("Content-Type", "text/html")
  8. w.Write([]byte(`
  9. Hello World! <br />
  10. <img src="/data/img/gopher.png" />
  11. `))
  12. }
  13. func main() {
  14. http.HandleFunc("/", handler)
  15. staticHandler := http.StripPrefix(
  16. "/data/",
  17. http.FileServer(http.Dir("./static")),
  18. )
  19. http.Handle("/data/", staticHandler)
  20. fmt.Println("starting server at :8080")
  21. http.ListenAndServe(":8080", nil)
  22. }