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的更多相关文章

  1. golang自定义struct字段标签

    原文链接: https://sosedoff.com/2016/07/16/golang-struct-tags.html struct是golang中最常使用的变量类型之一,几乎每个地方都有使用,从 ...

  2. golang 自定义json解析

    在实际开发中,经常会遇到需要定制json编解码的情况. 比如,按照指定的格式输出json字符串, 又比如,根据条件决定是否在最后的json字符串中显示或者不显示某些字段. 如果希望自己定义对象的编码和 ...

  3. golang 自定义类型的排序sort

    sort包中提供了很多排序算法,对自定义类型进行排序时,只需要实现sort的Interface即可,包括: func Len() int {... } func Swap(i, j int) {... ...

  4. golang 自定义time.Time json输出格式

    工作中使用golang时,遇到了一个问题.声明的struct含time.Time类型.使用json格式化struct时,time.Time被格式化成”2006-01-02T15:04:05.99999 ...

  5. golang 自定义封包协议(转的)

    package protocol import ( "bytes" "encoding/binary" ) const ( ConstHeader = &quo ...

  6. golang自定义路由控制实现(一)

        由于本人之前一直是Java Coder,在Java web开发中其实大家都很依赖框架,所以当在学习Golang的时候,自己便想着在Go开发中脱离框架,自己动手造框架来练习.通过学习借鉴Java ...

  7. Golang自定义包导入

    # 文件Tree project -/bin -/pkg -/src -main.go -/test -test1.go -test2.go main.go package main import ( ...

  8. golang自定义error

    系统自身的error处理一般是 errors.New()或fmt.Errorf()等,对一些需要复杂显示的,不太友好,我们可以扩展下error. error在标准库中被定义为一个接口类型,该接口只有一 ...

  9. golang自定义路由控制实现(二)-流式注册接口以及支持RESTFUL

        先简单回顾一下在上一篇的文章中,上一篇我主要是结合了数组和Map完成路由映射,数组的大小为8,下标为0的代表Get方法,以此类推,而数组的值则是Map,键为URL,值则是我们编写对应的接口.但 ...

随机推荐

  1. PHP函数总结 (三)

    <?php/** * PHP变量的范围 * 1.局部变量(内部变量) * 在函数内部声明的变量,作用域仅限于函数内部,参数也是局部变量:执行完毕后函数内部的变量都被释放 * 若需要使用函数内的变 ...

  2. 『PyTorch』第五弹_深入理解autograd_下:函数扩展&高阶导数

    一.封装新的PyTorch函数 继承Function类 forward:输入Variable->中间计算Tensor->输出Variable backward:均使用Variable 线性 ...

  3. Mac搭建个人服务器开发微信公众号

    个人电脑搭建小型服务器面临的一个重要问题就是网络问题,我把解决的过程简单记录一下,准备以后参考. 1. 内网穿透 由于个人所在网络是一个局域网,没有公网的IP,因此在公网是不能直接访问个人电脑的服务器 ...

  4. 无法使用BIPublisher开发报表

    我的机器是windows7,word版本问word 2010 32bit. 以前BIPublisher(安装的是BIPublisher 10.1.33版本)能正常使用,突然有一天再想使用他的时候,报以 ...

  5. 使用kubeadm安装Kubernetes v1.10

    关于K8S: Kubernetes是Google开源的容器集群管理系统.它构建于docker技术之上,为容器化的应用提供资源调度.部署运行.服务发现.扩 容缩容等整一套功能,本质上可看作是基于容器技术 ...

  6. 用Maven创建第一个web项目

    http://www.cnblogs.com/leiOOlei/p/3361633.html 一.创建项目 1.Eclipse中用Maven创建项目 上图中Next 2.继续Next 3.选maven ...

  7. 微信小程序wx.chooseImage和wx.previewImage的综合使用(图片上传不限制最多张数)

    WXML: <view class="weui"> <view class="weui-uploader"> <view clas ...

  8. POJ 2289 Jamie's Contact Groups 二分图多重匹配 难度:1

    Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 6511   Accepted: ...

  9. 搜索评价指标——NDCG

      ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:https://www.cnblogs.com/by-dream/p/9403984.html 概念 NDCG,Normali ...

  10. python安装大型包时出现错误Unable to find vcvarsall.bat

    在windows平台上,据说是安装cpython编写的包时会出现Unable to find vcvarsall.bat这种错误,缺失编译C的环境或组件吧,所以这个包就安装不成功,这个时候简单的方法就 ...