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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号