第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在 Gin 中添加具有相同路徑前綴和參數(shù)的不同端點(diǎn)?

如何在 Gin 中添加具有相同路徑前綴和參數(shù)的不同端點(diǎn)?

Go
天涯盡頭無女友 2022-10-24 08:58:04
我已經(jīng)在Go使用該Gin框架時(shí)構(gòu)建了一個(gè) API。我在 API 中有一條路線,可以讓用戶使用id. 但現(xiàn)在我也想通過用戶名獲取用戶。所以我嘗試了與讓用戶使用id. 但它在編譯期間給了我一個(gè)錯(cuò)誤。你們能告訴我我該怎么做嗎?謝謝你。路線 -grp.GET("/users", controllers.GetUsers)grp.GET("/users/:id", controllers.GetUser)grp.GET("/users/:username", controllers.GetUserByUsername)   //error - panic: ':username' in new path '/users/:username' conflicts with existing wildcard ':id' in existing prefix '/users/:id'grp.POST("/users", controllers.CreateUser)grp.PATCH("/users/:id", controllers.UpdateUser)grp.DELETE("/users/:id", controllers.DeleteUser)控制器 -func GetUser(c *gin.Context) {    paramID := c.Params.ByName("id")    ctx := context.Background()    sa := option.WithCredentialsFile(firestoreFilePath)    app, err := firebase.NewApp(ctx, nil, sa)    if err != nil {        log.Fatalln(err)    }    client, err := app.Firestore(ctx)    if err != nil {        log.Fatalln(err)    }    defer client.Close()    dsnap, err := client.Collection("users").Doc(paramID).Get(ctx)    if err != nil {        fmt.Print(err)        c.IndentedJSON(http.StatusNotFound, gin.H{            "message": "User not found",        })        return    }    m := dsnap.Data()    c.IndentedJSON(http.StatusNotFound, gin.H{        "User":    m,        "message": "User returned successfully",    })}API 響應(yīng) -[  {     "id": 1,     "name": "Leanne Graham",     "username": "Bret",     "email": "Sincere@april.biz",  },  {    "id": 2,    "name": "Ervin Howell",    "username": "Antonette",    "email": "Shanna@melissa.tv",  },  {    "id": 3,    "name": "Clementine Bauch",    "username": "Samantha",    "email": "Nathan@yesenia.net",  }]
查看完整描述

1 回答

?
MMTTMM

TA貢獻(xiàn)1869條經(jīng)驗(yàn) 獲得超4個(gè)贊

這是杜松子酒的一個(gè)已知限制。您必須使所有路徑都獨(dú)一無二。就像添加如下前綴一樣:

grp.GET("/users/username/:username", controllers.GetUserByUsername)

有關(guān)此問題線程的更多信息: https ://github.com/gin-gonic/gin/issues/1301


查看完整回答
反對(duì) 回復(fù) 2022-10-24
  • 1 回答
  • 0 關(guān)注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)