转载

golang 静态文件资源服务器设置2 Main.go代码<embed wmode="transparent" src="https://qq4668620...

  1. package main

  2. import "net/http"

  3. import "io"

  4. import "os"

  5. import "log"

  6. type HttpHander struct{}

  7. type Mstring string

  8. func main() {

  9. mux := http.NewServeMux()

  10. mux.Handle("/", HttpHander{})

  11. //获取当前路径

  12. wd, err := os.Getwd()

  13. if err != nil {

  14. log.Fatalln(err)

  15. }

  16. //func StripPrefix(prefix string, h Handler) Handler

  17. // 给定url 删除前缀

  18. mux.Handle("/static/", http.StripPrefix("/static", http.FileServer(http.Dir(wd))))

  19. http.HandleFunc("/", sayHello)

  20. http.ListenAndServe(":8080", mux)

  21. }

  22. func (HttpHander) ServeHTTP(response http.ResponseWriter, request *http.Request) {

  23. io.WriteString(response, "ServeHTTP")

  24. }

  25. func sayHello(response http.ResponseWriter, request *http.Request) {

  26. io.WriteString(response, "sayHello")

  27. }

地址栏输入: http://localhost:8080/static/ **

原文  https://studygolang.com/articles/18114
正文到此结束
Loading...