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--100--相同的树
问题描述: 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1 / \ / \ 2 3 2 3 [1,2, ...
- jenkins+python+kubectl实现批量更新k8s镜像
一.jenkins使用官方镜像(官方镜像默认是jenkins用户,权限太低) 因此通过dockerfile重新改为root启动 FROM jenkins/jenkins:lts MAINTAINER ...
- _beginthreadex()和CreateThread()的区别
在本例子中我们使用——beginThreadex这个函数,它和createThread的区别是: 为了方便管理,我么在使用该函数的时候可以把它的线程函数作为类成员,这也就需要在类中把该函数变成静态函数 ...
- JAVA System.arraycopy 和Arrays.copyof 效率比较
System.arraycopy()源码.可以看到是native方法: native关键字说明其修饰的方法是一个原生态方法,方法对应的实现不是在当前文件,而是在用其他语言(如C和C++)实现的文件中. ...
- sql Server如何执行批量插入和批量删除
平时我们sql server执行查询语句都是通过 insert into 表名(字段名,字段名) values(插入值,插入值) --单条插入语句--- insert into Reader(read ...
- linux abstract model of virtual memory
- struts2 的学习
1.struts.xml 中的action不是代表一个action类(控制器),而是代表一次访问,所以action的name属性和访问路径有关.配置文件中的package的namespace属性其实就 ...
- SystemInfo获取计算机相关信息
// SystemInfo.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"#include <iostream>#include ...
- MyEclipse持续性开发教程:用JPA和Spring管理数据(一)
MyEclipse开年钜惠 在线购买低至75折!立即开抢>> [MyEclipse最新版下载] 本教程介绍了MyEclipse中的一些基于JPA / Spring的功能.有关设置JPA项目 ...
- SharePoint Excel Service-PowerShell
1. 创建一个信任的文件路径 New-SPExcelFileLocation -Address "http://sp2010/Excel Workbooks" -ExcelServ ...