Go Fiber依赖版本冲突致utils.GetTrimmedParam未定义编译错误
问题:无法访问/api/user且编译报错
undefined: utils.GetTrimmedParam 终端错误输出
# github.com/gofiber/fiber ..\..\..\go\pkg\mod\github.com\gofiber\fiber@v1.14.6\path.go:175:21: undefined: utils.GetTrimmedParam failed to build, error: exit status 1 # github.com/gofiber/fiber ..\..\..\go\pkg\mod\github.com\gofiber\fiber@v1.14.6\path.go:175:21: undefined: utils.GetTrimmedParam # github.com/gofiber/fiber ..\..\..\go\pkg\mod\github.com\gofiber\fiber@v1.14.6\path.go:175:21: undefined: utils.GetTrimmedParam # github.com/gofiber/fiber ..\..\..\go\pkg\mod\github.com\gofiber\fiber@v1.14.6\path.go:175:21: undefined: utils.GetTrimmedParam # github.com/gofiber/fiber ..\..\..\go\pkg\mod\github.com\gofiber\fiber@v1.14.6\path.go:175:21: undefined: utils.GetTrimmedParam
authController.go 代码
package controllers import ( "strconv" "github.com/ADEMOLA200/go-admin/database" "github.com/ADEMOLA200/go-admin/models" "github.com/dgrijalva/jwt-go" "github.com/gofiber/fiber/v2" "golang.org/x/crypto/bcrypt" "time" ) func Register(c *fiber.Ctx) error { var data map[string]string if err := c.BodyParser(&data); err != nil { return err } if data["password"] != data["password-confirm"] { c.Status(400) return c.JSON(fiber.Map{ "message": "password do not match", }) } /********************** HASH PASSWORD ********************************************/ password, _ := bcrypt.GenerateFromPassword([]byte(data["password"]), 14) /*********************************************************************************/ user := models.User{ FirstName: data["first_name"], LastName: data["last_name"], Email: data["email"], Password: password, } database.DB.Create(&user) return c.JSON(user) } func Login(c *fiber.Ctx) error { var data map[string]string if err := c.BodyParser(&data); err != nil { return err } var user models.User database.DB.Where("email = ?", data["email"]).First(&user) if user.Id == 0 { c.Status(404) return c.JSON(fiber.Map{ "message": "incorrect email or no user found", }) } if err := bcrypt.CompareHashAndPassword(user.Password, []byte(data["password"])); err != nil { c.Status(400) return c.JSON(fiber.Map{ "message": "incorrect password", }) } claims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.StandardClaims{ Issuer: strconv.Itoa(int(user.Id)), ExpiresAt: time.Now().Add(time.Hour * 24).Unix(), }) token, err := claims.SignedString([]byte("secret")) if err != nil { return c.SendStatus(fiber.StatusInternalServerError) } cookie := fiber.Cookie{ Name: "jwt", Value: token, Expires: time.Now().Add(time.Hour * 24), HTTPOnly: true, } c.Cookie(&cookie) return c.JSON(fiber.Map{ "message": "success", }) } type Claims struct { jwt.StandardClaims } func User(c *fiber.Ctx) error { cookie := c.Cookies("jwt") token, err := jwt.ParseWithClaims(cookie, &Claims{}, func(token *jwt.Token) (interface{}, error) { return []byte("secret"), nil }) if err != nil || !token.Valid { c.Status(fiber.StatusUnauthorized) return c.JSON(fiber.Map{ "message": "unauthenticated", }) } claims := token.Claims return c.JSON(claims) }
go.mod 代码
module github.com/ADEMOLA200/go-admin go 1.20 require ( github.com/gofiber/cors v0.2.2 github.com/gofiber/fiber/v2 v2.48.0 golang.org/x/crypto v0.11.0 gorm.io/driver/mysql v1.5.1 gorm.io/gorm v1.25.2 ) require ( github.com/gofiber/fiber v1.14.6 // indirect github.com/gofiber/utils v1.1.0 // indirect github.com/gorilla/schema v1.2.0 // indirect ) require ( github.com/andybalholm/brotli v1.0.5 // indirect github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/go-sql-driver/mysql v1.7.1 // indirect github.com/google/uuid v1.3.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/klauspost/compress v1.16.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.19 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect github.com/rivo/uniseg v0.4.4 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.48.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect golang.org/x/sys v0.10.0 // indirect )
解决方案
问题根源
项目同时存在Fiber v2(业务代码使用的正确版本)和Fiber v1(间接引入的旧版本),v1.14.6版本的Fiber依赖的github.com/gofiber/utils版本与当前v1.1.0不兼容,GetTrimmedParam函数在该utils版本中不存在,导致编译失败。
修复步骤
- 移除Fiber v1的间接依赖
go get github.com/gofiber/fiber@none - 清理Go模块缓存
go clean -modcache - 重新整理依赖
go mod tidy - 确认项目导入
检查所有代码文件,确保导入的Fiber包都是github.com/gofiber/fiber/v2,没有引用v1版本的github.com/gofiber/fiber。
执行完以上步骤后重新编译项目,即可解决编译报错,之后再测试/api/user接口的访问问题。
内容的提问来源于stack exchange,提问作者Abdullah odukoya
相关产品推荐
相关产品推荐

