加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang 自定义importpath

发布时间:2020-12-16 09:34:08 所属栏目:大数据 来源:网络整理
导读:golang 的包导入和其他语言有好多不一样的地方,以下是一个自定义的导入 golang 自定义导入说明 一个官方的说明 比较简单,就不翻译了,主要是说我们可以通过添加meta 数据告诉包如何进行加载 For example,import "example.org/pkg/foo"will result in the f

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读