gogo-gin

What is difference between router.Static() and router.Use(static.Serve()) on the gin?


I was just reading the documentation of gin and found there are 2 different way to set assets folder to the server, one is using Static() method of the router as follow:

package main

import "github.com/gin-gonic/gin"

func main() {
        r := gin.Default()
        r.Static("/assets", "./assets")

Then the other is using static middleware as follows:

package main

import (
       "github.com/gin-gonic/gin"
       "github.com/gin-contrib/static"
)

func main() {
        r := gin.Default()
        r.Use(static.Serve("/", static.LocalFile("./assets", false)))

Is there any difference between these 2 ways about speed, side-effect, and so on?


Solution

  • First option from:

    Static serves files from the given file system root. Internally a http.FileServer is used, therefore http.NotFound is used instead of the Router's NotFound handler.

    Second option link:

    Static returns a middleware handler that serves static files in the given directory.

    both uses http.FileServer under the hood.