gin
gin copied to clipboard
docs: fix route group example code
I have identified a couple of issues with the current example code for route groups in the documentation. Below are the details of these issues and the proposed fix.
Issues
In the current example code, there is a risk of making a typo that can lead to unintended behavior:
func main() {
router := gin.Default()
// Simple group: v1
v1 := router.Group("/v1")
{
v1.POST("/login", loginEndpoint)
v1.POST("/submit", submitEndpoint)
v1.POST("/read", readEndpoint)
}
// Simple group: v2
v2 := router.Group("/v2")
{
v2.POST("/login", loginEndpoint)
v2.POST("/submit", submitEndpoint)
- v2.POST("/read", readEndpoint)
+ v1.POST("/read", readEndpoint)
}
router.Run(":8080")
}
In the code above, the intention is to add endpoints to the v2 group, but due to a typo, the endpoint might accidentally be added to the v1 group instead. This issue is problematic because the code will compile and run without errors, potentially leading to confusion for beginners.
Proposed Fix
By making the following changes, we can ensure that route groups are clearly separated and reduce the risk of accidentally adding endpoints to the wrong group:
func main() {
router := gin.Default()
// Simple group: v1
- v1 := router.Group("/v1")
{
+ v1 := router.Group("/v1")
v1.POST("/login", loginEndpoint)
v1.POST("/submit", submitEndpoint)
v1.POST("/read", readEndpoint)
}
// Simple group: v2
- v2 := router.Group("/v2")
{
+ v2 := router.Group("/v2")
v2.POST("/login", loginEndpoint)
v2.POST("/submit", submitEndpoint)
v2.POST("/read", readEndpoint)
}
router.Run(":8080")
}
This change will clearly separate the endpoints for each version, helping to prevent the mistake of adding endpoints to the wrong version.
I believe that incorporating this fix into the documentation will help users who are new to Gin avoid this issue. If this pull request is accepted, I also plan to submit a similar update to the gin-gonic/website repository.
Thank you for considering this request.
This version provides a clear explanation of the problem and the proposed solution, making it easy for reviewers to understand the importance of the change and its impact.
Codecov Report
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 99.06%. Comparing base (
3dc1cd6) to head (113676a). Report is 70 commits behind head on master.
Additional details and impacted files
@@ Coverage Diff @@
## master #4020 +/- ##
==========================================
- Coverage 99.21% 99.06% -0.16%
==========================================
Files 42 44 +2
Lines 3182 2772 -410
==========================================
- Hits 3157 2746 -411
+ Misses 17 15 -2
- Partials 8 11 +3
| Flag | Coverage Δ | |
|---|---|---|
? |
||
| -tags "sonic avx" | 99.05% <ø> (?) |
|
| -tags go_json | 99.05% <ø> (?) |
|
| -tags nomsgpack | 99.04% <ø> (?) |
|
| go-1.18 | ? |
|
| go-1.19 | ? |
|
| go-1.20 | ? |
|
| go-1.21 | 99.06% <ø> (-0.16%) |
:arrow_down: |
| go-1.22 | 99.06% <ø> (?) |
|
| macos-latest | 99.06% <ø> (-0.16%) |
:arrow_down: |
| ubuntu-latest | 99.06% <ø> (-0.16%) |
:arrow_down: |
Flags with carried forward coverage won't be shown. Click here to find out more.
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
LGTM