Golang import path best practice
来源:Golang技术栈
时间:2023-04-01 08:32:32 355浏览 收藏
珍惜时间,勤奋学习!今天给大家带来《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:
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学习网公众号。
-
439 收藏
-
262 收藏
-
193 收藏
-
188 收藏
-
500 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 507次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习