Golang import path best practice
珍惜时间,勤奋学习!今天给大家带来《Golang import path best practice》,正文内容主要涉及到golang等等,如果你正在学习Golang,或者是对Golang有疑问,欢迎大家关注我!后面我会持续更新相关内容的,希望都能帮到正在学习的大家!
问题内容
I am currently working on a private project using Golang (and I am new to it).
But I have the problem that I don't know what is the proper way to define the
import path
for local packages.
I have the same feeling as the author from this link https://medium.com/@c9s/golang-the-annoying-remote-import-path-c6c7e76517e5
In short, if I host a project foo
in Github. I feel strange to use
github.com/levin/foo
as import path instead of just foo
. Wouldn't it cause
much rework if I move my codes to Bitbucket or I host my own git server in
AWS?
And worse, I can rewrite my code to change the import path, but how would people using my code in theirs notify the change of repo? And I feel no sense to ask others to update their codes.
I am new to Golang so feel free to say something like "your question is not even valid".
正确答案
The answer to your question:
I don't know what is the proper way to define the import path for local packages.
As @JimB said:
If you want to use the Go tools you need to follow the conventions. If you want go get to work, then you need to import it with the full name, otherwise put it wherever you want in GOPATH
So you need to use the complete import path github.com/levin/foo
if you want
go get
to work, and you should do it this way if you expect other people to
use your package.
The answer to your second question:
Wouldn't it cause much rework if I move my codes to Bitbucket or I host my own git server in AWS?
There is a way to use a custom domain name as import path and still host your code wherever you want, it's called a vanity import path I think. You just need to add some meta tags to the html file that gets served in the import file that you use.
This article
explains how to do it, but in summary, in the file that gets served in your
custom domain when the custom import path is accessed, you need to add a go- import
meta tag that points to where you hosted the code. The article uses
github.com/foo/bar
as example of where you are hosting your code and
foo.com/bar
as your real import path.
So in the file that gets served when accessing foo.com/bar
there should be a
meta tag like this:
<meta name="go-import" content="golang.org/x/tools git http://github.com/foo/bar">
And you continue to host your code in github. Then if you change the hosting
place to your code you just change the value of the meta tag, but all the code
that uses the package continue to use the exact same import path
"foo.com/bar
.
The only issue with this is that now your project can get imported by 2
different paths: foo.com/bar
and github.com/foo/bar
. To fix this they have
canonical imports which only
allow the package to be imported using the custom path and not the github one,
it's only adding a comment next to the package name on each package file:
package bar // import "foo.com/bar"
This is the only way to avoid the issue you have. You can use @srxf answer if
you are using a package that is just going to be used locally just know that
the go tools are not going to work with that code (like go get
). If you want
the code to work as it is intended then this is the way to go.
As a comment, I know it feels silly importing github.com/levin/foo
for a
local package, but if you use that package in another package (say foo2
) and
someone else imports foo2
, this allows the compiler to know exactly where to
get the dependencies for foo2
, because all the import in the code include
the whole route, not the name of a local file. This way people can always get
the dependencies they need for your package without having to include those
files in your repo, and without having to configure anything for it to work.
It's not perfect but it's the way go works, they call it convention over
configuration or something like that.
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

- 上一篇
- 如何用冒号解组 XML 属性?

- 下一篇
- Is it possible to post coverage for multiple packages to Coveralls?
-
- 开心的枫叶
- 这篇文章出现的刚刚好,太详细了,受益颇多,收藏了,关注老哥了!希望老哥能多写Golang相关的文章。
- 2023-06-04 07:07:40
-
- 风趣的天空
- 细节满满,码住,感谢作者大大的这篇技术文章,我会继续支持!
- 2023-05-29 23:07:51
-
- 标致的镜子
- 很好,一直没懂这个问题,但其实工作中常常有遇到...不过今天到这,看完之后很有帮助,总算是懂了,感谢大佬分享文章!
- 2023-04-26 00:20:26
-
- 多情的学姐
- 这篇文章太及时了,up主加油!
- 2023-04-19 06:19:32
-
- Golang · Go问答 | 1年前 |
- 在读取缓冲通道中的内容之前退出
- 139浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 戈兰岛的全球 GOPRIVATE 设置
- 204浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何将结构作为参数传递给 xml-rpc
- 325浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何用golang获得小数点以下两位长度?
- 478浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何通过 client-go 和 golang 检索 Kubernetes 指标
- 486浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 将多个“参数”映射到单个可变参数的习惯用法
- 439浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 将 HTTP 响应正文写入文件后出现 EOF 错误
- 357浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 结构中映射的匿名列表的“复合文字中缺少类型”
- 352浏览 收藏
-
- Golang · Go问答 | 1年前 |
- NATS Jetstream 的性能
- 101浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何将复杂的字符串输入转换为mapstring?
- 440浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 相当于GoLang中Java将Object作为方法参数传递
- 212浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何确保所有 goroutine 在没有 time.Sleep 的情况下终止?
- 143浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 511次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 498次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
-
- 千音漫语
- 千音漫语,北京熠声科技倾力打造的智能声音创作助手,提供AI配音、音视频翻译、语音识别、声音克隆等强大功能,助力有声书制作、视频创作、教育培训等领域,官网:https://qianyin123.com
- 100次使用
-
- MiniWork
- MiniWork是一款智能高效的AI工具平台,专为提升工作与学习效率而设计。整合文本处理、图像生成、营销策划及运营管理等多元AI工具,提供精准智能解决方案,让复杂工作简单高效。
- 92次使用
-
- NoCode
- NoCode (nocode.cn)是领先的无代码开发平台,通过拖放、AI对话等简单操作,助您快速创建各类应用、网站与管理系统。无需编程知识,轻松实现个人生活、商业经营、企业管理多场景需求,大幅降低开发门槛,高效低成本。
- 111次使用
-
- 达医智影
- 达医智影,阿里巴巴达摩院医疗AI创新力作。全球率先利用平扫CT实现“一扫多筛”,仅一次CT扫描即可高效识别多种癌症、急症及慢病,为疾病早期发现提供智能、精准的AI影像早筛解决方案。
- 103次使用
-
- 智慧芽Eureka
- 智慧芽Eureka,专为技术创新打造的AI Agent平台。深度理解专利、研发、生物医药、材料、科创等复杂场景,通过专家级AI Agent精准执行任务,智能化工作流解放70%生产力,让您专注核心创新。
- 104次使用
-
- 老师代码没有自动跟踪?
- 2023-03-07 439浏览
-
- c程序fork并等待golang进程状态
- 2023-03-05 262浏览
-
- GOLANG使用Context管理关联goroutine的方法
- 2022-12-28 193浏览
-
- 怎么用Golang将MySQL表转储为JSON
- 2023-03-07 188浏览
-
- Golang 检查字符串是否为有效路径?
- 2023-03-10 500浏览