【发布时间】:2020-08-13 17:00:47
【问题描述】:
按照教程我尝试将前端(React)连接到后端API(Gin),但是static.Serve不起作用,错误提示如下:
cannot use static.Serve("/", static.LocalFile("./views", true)) (type "github.com/gin-gonic/gin".HandlerFunc) as type "github.com/supebirdgz/amgmt/vendor/github.com/gin-gonic/gin".HandlerFunc in argument to router.Use
来源:
import (
"github.com/gin-gonic/gin"
"github.com/gin-gonic/contrib/static"
)
func main() {
router := gin.Default()
router.Use(static.Serve("/", static.LocalFile("./frontend", true)))
router.Run()
}
Gin 有更新吗?我试过用别人替换静态包,还是一样。
【问题讨论】:
-
确保您同时销售了
gin和contrib/static。该错误表明Use需要来自"github.com/supebirdgz/amgmt/vendor/github.com/gin-gonic/gin"(供应商包)的handlerfunc 类型,但您传递的是来自"github.com/gin-gonic/gin"(非供应商包)的一个。这表明您只销售了gin包,而不是contrib/static包。 -
@mkopriva 非常感谢,我检查了“govendor list”并确实找到了非供应商包,添加后它可以工作。