Serving static files
Gin provides three methods for serving static content:
router.Static(relativePath, root)— Serves an entire directory. Requests torelativePathare mapped to files underroot. For example,router.Static("/assets", "./assets")serves./assets/style.cssat/assets/style.css.router.StaticFS(relativePath, fs)— LikeStatic, but accepts anhttp.FileSysteminterface, giving you more control over how files are resolved. Use this when you need to serve files from an embedded filesystem or want to customize directory listing behavior.router.StaticFile(relativePath, filePath)— Serves a single file. Useful for endpoints like/favicon.icoor/robots.txt.
package main
import ( "net/http"
"github.com/gin-gonic/gin")
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")}