golang 自定义importpath
golang 的包导入和其他语言有好多不一样的地方,以下是一个自定义的导入
golang 自定义导入说明
- 一个官方的说明
比较简单,就不翻译了,主要是说我们可以通过添加meta 数据告诉包如何进行加载
For example,
import "example.org/pkg/foo"
will result in the following requests:
https://example.org/pkg/foo?go-get=1 (preferred)
http://example.org/pkg/foo?go-get=1 (fallback, only with -insecure)
If that page contains the meta tag
<meta name="go-import" content="example.org git https://code.org/r/p/exproj">
the go tool will verify that https://example.org/?go-get=1 contains the
same meta tag and then git clone https://code.org/r/p/exproj into
GOPATH/src/example.org.
- 参考
package main
import (
"log"
// 自定义的地址,实际是从github 导入包
"rongdemo.com"
)
func main() {
log.Println(shortid.Generate())
}
具体操作
- 修改hosts(我没有rongdemo.com的域名),同时我使用的说本机
/etc/hosts
127.0.0.1 rongdemo.com
- 添加一个静态web站点,并添加meta 数据
yarn inti -y
yarn add live-server --dev
修改package.json
{
"name": "golang-web-package",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"live-server": "^1.2.0"
},
"scripts": {
"start": "live-server"
}
}
nano index.html
<html>
<meta name="go-import" content="rongdemo.com git https://github.com/teris-io/shortid" />
<body>
golang package
</body>
</html>
- 启动&&使用dep 管理包
yarn start
dep init
- 效果

参考实现
gopkg.in
实现原理也是类似的
参考资料
https://github.com/niemeyer/gopkg
https://golang.org/cmd/go/#hdr-Remote_import_paths
https://github.com/rongfengliang/golang-customimport
https://github.com/golang/gddo/wiki/Source-Code-Links
golang 自定义importpath的更多相关文章
- golang自定义struct字段标签
原文链接: https://sosedoff.com/2016/07/16/golang-struct-tags.html struct是golang中最常使用的变量类型之一,几乎每个地方都有使用,从 ...
- golang 自定义json解析
在实际开发中,经常会遇到需要定制json编解码的情况. 比如,按照指定的格式输出json字符串, 又比如,根据条件决定是否在最后的json字符串中显示或者不显示某些字段. 如果希望自己定义对象的编码和 ...
- golang 自定义类型的排序sort
sort包中提供了很多排序算法,对自定义类型进行排序时,只需要实现sort的Interface即可,包括: func Len() int {... } func Swap(i, j int) {... ...
- golang 自定义time.Time json输出格式
工作中使用golang时,遇到了一个问题.声明的struct含time.Time类型.使用json格式化struct时,time.Time被格式化成”2006-01-02T15:04:05.99999 ...
- golang 自定义封包协议(转的)
package protocol import ( "bytes" "encoding/binary" ) const ( ConstHeader = &quo ...
- golang自定义路由控制实现(一)
由于本人之前一直是Java Coder,在Java web开发中其实大家都很依赖框架,所以当在学习Golang的时候,自己便想着在Go开发中脱离框架,自己动手造框架来练习.通过学习借鉴Java ...
- Golang自定义包导入
# 文件Tree project -/bin -/pkg -/src -main.go -/test -test1.go -test2.go main.go package main import ( ...
- golang自定义error
系统自身的error处理一般是 errors.New()或fmt.Errorf()等,对一些需要复杂显示的,不太友好,我们可以扩展下error. error在标准库中被定义为一个接口类型,该接口只有一 ...
- golang自定义路由控制实现(二)-流式注册接口以及支持RESTFUL
先简单回顾一下在上一篇的文章中,上一篇我主要是结合了数组和Map完成路由映射,数组的大小为8,下标为0的代表Get方法,以此类推,而数组的值则是Map,键为URL,值则是我们编写对应的接口.但 ...
随机推荐
- LeetCode--125--验证回文串
问题描述: 好 times out: class Solution(object): def isPalindrome(self, s): """ :type s: st ...
- Android程序员眼中世界上最遥远的距离
世界上最遥远的距离,是我在if里你在else里,似乎一直相伴又永远分离: 世界上最痴心的等待,是我当case你是switch,或许永远都选不上自己: 世界上最真情的相依,是你在try我在catch. ...
- 开发环境运行正常,发布服务器后提示HTTP 错误 403.14 - Forbidden
一.发布服务器后报错 今天在项目发布中遇到一件奇怪的事,开发完成的项目,发布到服务器上时 1. 发布到A服务器,一切正常 2. 发布到B服务器,提示403服务器错误 在同事电脑上重新打包发布代码,并发 ...
- 『cs231n』作业3问题2选讲_通过代码理解LSTM网络
LSTM神经元行为分析 LSTM 公式可以描述如下: itftotgtctht=sigmoid(Wixxt+Wihht−1+bi)=sigmoid(Wfxxt+Wfhht−1+bf)=sigmoid( ...
- UVA-10726 Coco Monkey(递推)
题目大意:n个人,m个猴子分桃,第一个人把桃子分成n份余下m个,第一个人将余下的给猴子,拿走自己的那份.第二个人把剩下的桃子也分成n份,余下m个,将余下的分给猴子,拿走自己的那份.………… 直到n个人 ...
- 78. Subsets 90. Subsets II
1. Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...
- dubbo provider如何对invoker进行export
如何把provider的invoker export出去:1)为原始对象加wrapper,生成invoker:2)给invoker加各种filter,启动监听服务:3)注册服务地址 以HelloSer ...
- splunk中mongodb作用——存用户相关数据如会话、搜索结果等
About the app key value store The app key value store (or KV store) provides a way to save and retri ...
- WindowsLiveWriter安装步骤
WindowsLiveWriter 安装WindowsLiveWriter.exe http://www.cnblogs.com/rhxuza1993/
- 从用户输入url到页面最后呈现 发生了些什么?
一.浏览器获取资源的过程: 1.输入url 2.浏览器解析url,获得主机名 3.将主机名转换成服务器ip地址(查找本地DNS缓存列表,如果没有则向默认的DNS服务器发送查询请求) 4.浏览器建立一条 ...