管理硬编码的导入路径

In Go, it is common that some packages are versioned. So a program might look like this:

package main

import (
    "github.com/go-gl/gl/v3.3-core/gl"
    "github.com/go-gl/glfw/v3.2/glfw"
)

// ... do stuff

Sometimes, I might want to update the version of glfw. Lets imagine GLFW 3.3 bindings come to Go and I want to update from 3.2.

I might have multiple Go files in a project all using glfw. I don't want to go into each of them and update the version of the import by hand. Ideally I wouldn't be copying that long path around, either, and I could define it in one place per project.

  • Maybe I could write a script to find+replace "github.com/go-gl/glfw/v3.2/glfw"
  • Maybe I could template the file with Genny
  • Maybe I could create a symlink inside the root Go path "glfw" -> "github.com/go-gl/glfw/v3.2/glfw", update it when changing version, and just use import "glfw"
    • but this information then lives "outside" the project, so no-one cloning my project knows what version to use
    • but this is a global change and I might have multiple projects which want to depend on different versions

Ideally I would be able to do something like this in each source file:

package main

import (
    $gl
    $glfw
)

And in some project-level dot file, something like:

gl=github.com/go-gl/gl/v3.3-core/gl
glfw=github.com/go-gl/glfw/v3.2/glfw

Or, a command-line argument attached to go build defining constants that could look something like:

go build -Dgl=github.com/go-gl/gl/v3.3-core/gl -Dglfw=github.com/go-gl/glfw/v3.2/glfw

How is everyone else handling this currently?

See github.com/golang/go/wiki/Modules for the recommended way of managing package versions.