Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: fix route group example code #4020

Merged
merged 1 commit into from
Sep 15, 2024

Conversation

demouth
Copy link
Contributor

@demouth demouth commented Jul 30, 2024

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.

Copy link

codecov bot commented Jul 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

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%) ⬇️
go-1.22 99.06% <ø> (?)
macos-latest 99.06% <ø> (-0.16%) ⬇️
ubuntu-latest 99.06% <ø> (-0.16%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@appleboy appleboy added this to the v1.11 milestone Sep 15, 2024
@appleboy
Copy link
Member

LGTM

@appleboy appleboy merged commit f2c861a into gin-gonic:master Sep 15, 2024
24 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants