douwen5985 2019-09-15 23:12
浏览 36
已采纳

管理硬编码的导入路径

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?

  • 写回答

1条回答 默认 最新

  • dongqiu7365 2019-09-15 23:29
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动