我应该在程序包级别但在http处理程序之外声明变量吗?

I'm using gin gonic as http framework and I need to group some paths with shared variable by like this:

ur := r.Group("/")
ur.Use(package.Prepare)
{
    ur.GET("/", package.Home)
}

Inside the Prepare handler, I declare package variable like package.tplPath because I want all sub routes can access to this variable instead of rewrite the code in each http handler.

var tplPath = ""

func Prepare(c *gin.Context) {
    _, filename, _, _ := runtime.Caller(0)
    s := helper.RelativeFilePath(filename)
    tplPath = s[1:len(s)] + "template/"
}

I don't know how Go works with each process, and variables for each http request. If a variable was declared in package level, will it be set after each http request?

Is this considered good practice? If not, why not?

This is not threadsafe, package variables are shared across all goroutines, and modification in one routine will change the value in all the others, causing a data race.

In general; try and avoid using package level variables where possible.

Edit:

In go, packages are a kind of module. There exists only one instance of a package for any given import path (basically package name). This means that there is only 1 instance of any variable at package level.

Package variables are shared, global state. All accessors of that variable will be accessing the exact same memory.

It does not matter what type the package variable is, struct / string / int etc. If it is defined at package level, all accessors of that variable will share the same instance of it.

If (as with http servers) you have concurrent code, there will be multiple accessors of that variable at the same time. In some situations this is fine, like when that variable is only ever read, but it appears that in your case it will be modified. Making this code racy!

The tplPath is a global variable, all routine will access the same memory address, and set after each http request. If you want set just once, and the tplPath not depend on the http request. You can set it in the init function.

func init(){
    _, filename, _, _ := runtime.Caller(0)
    s := helper.RelativeFilePath(filename)
    tplPath = s[1:len(s)] + "template/"
}

The init function will run before main, and just do once.