http.HandleFunc("/upload",uploadHandler) //static file handler. http.Handle("/staticfile/",http.StripPrefix("/staticfile/",http.FileServer(http.Dir("./staticfile")))) //Listen on port 8080 http.ListenAndServe(":
static.LocalFile("./public",false)))// 其他路径 /other-place// r.Use(static.Serve("/other-place", static.LocalFile("./public", false)))r.GET("/ping",func(c*gin.Context){c.String(200,"test")})
router.Static("/assets","./assets") router.StaticFS("/more_static",http.Dir("my_file_system")) router.StaticFile("/favicon.ico","./resources/favicon.ico") // Listen and serve on 0.0.0.0:8080 router.Run(":8080") } 不过,这个例子中,官方只考虑到了静态资源都存放于二级目录,并且静态资源...
func main() { router := gin.Default() router.Static("/assets", "./assets") router.StaticFS("/more_static", http.Dir("my_file_system")) router.StaticFile("/favicon.ico", "./resources/favicon.ico") // Listen and serve on 0.0.0.0:8080 router.Run(":8080") } 1. 2. 3. 4. 5...
log.Fatal(http.ListenAndServe(":8989",nil)) } 例2:内嵌文件 — 命令行应用 简单的 Hello World: packagemainimport( _"embed""fmt")//go:embed message.txtvarmessagestringfuncmain(){ fmt.Println(message) } 其中messaeg.txt 中的内容是 Hello World。目录结构如下: ...
//static server http.FileServer(http.Dir("目录")) //Manually configure the service and routing s := &http.Server{ Addr: ":8080", Handler: myHandler, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, MaxHeaderBytes: 1 << 20, } s.ListenAndServe(); // path/filepath...
上面的代码首先是获取地址addr,然后执行fast-cgi,调用ListenAndServeTLS监听cgi服务,后面的是Http服务,调用ListenAndServe()监听http服务。 func ListenAndServe(addr string, handler Handler) error { server := &Server{Addr: addr, Handler: handler}returnserver.ListenAndServe() ...
(http.ListenAndServe(":9999",router)) }funcindexHandler(whttp.ResponseWriter,reqbunrouter.Request)error{returnindexTemplate().Execute(w,nil) }funcdebugHandler(whttp.ResponseWriter,reqbunrouter.Request)error{returnbunrouter.JSON(w, bunrouter.H{"route":req.Route(),"params":req.Params().Map(),...
package mainimport("embed""log""net/http")// content holds the static content (2 files) for the web server.//go:embed a.txt b.txtvar content embed.FSfuncmain(){ http.Handle("/", http.FileServer(http.FS(content))) log.Fatal(http.ListenAndServe(":8080",nil))} 完整的Playgroun...
router.ServeHTTP(w, req) ifw.Code !=200{ passed =false } } assert.Equal(t,true, passed) } 这部分代码,你可以从https://github.com/soulteary/awesome-golang-embed/tree/main/go-embed-official/testable中获得。 第二步:添加性能探针 以往针对黑盒程序,我们只能用监控和事前事后的对比来获取具体的...