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

Create link with router #3256

Open
asbjornu opened this issue Jul 27, 2022 · 2 comments · May be fixed by #3589
Open

Create link with router #3256

asbjornu opened this issue Jul 27, 2022 · 2 comments · May be fixed by #3589

Comments

@asbjornu
Copy link

asbjornu commented Jul 27, 2022

Description

In this blog post, it is stated that the URL for a particular route can be created on the fly with the following method:

url := router.Url("homeHandler")

However, trying to access the Url method yields the following error:

router.Url undefined (type *gin.Engine has no field or method Url)

In what seems to be a fork of Gin 1.7.4, I've found the URLFor function, but I'm unable to find either the containing file template_func.go or the function URLFor in Gin's history. If anyone knows whether such a function has ever existed or something similar to it currently does exist, please shout!

How to reproduce

package main

import (
	"github.com/gin-gonic/gin"
)

func home(c *gin.Context) {
	router := gin.Default()
	url := router.Url("home")
	c.String(200, url)
}

func main() {
	router := gin.Default()
	router.GET("/", home)
	router.Run(":9000")
}

Expectations

Given a gin.Context, I expect to be able to construct a route URL on a whim, anywhere in my application.

Actual result

router.Url is not a method, so it fails:

router.Url undefined (type *gin.Engine has no field or method Url)

Environment

  • go version: go1.17.5 darwin/amd64
  • gin version (or commit ref): v1.8.1
  • operating system: macOS 11.6.8 Big Sur
@zy2324
Copy link

zy2324 commented Jul 29, 2022

The blog is gin.New() , and you are only gin.Default(), I am not sure if it is affect or not

@asbjornu
Copy link
Author

@zy2324, it doesn't affect which methods are available on the gin.Engine whether it's instantiated by gin.New() or gin.Default().

@asbjornu asbjornu linked a pull request Apr 30, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants