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

 
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值