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://www.cnblogs.com/rongfengliang/p/9529994.html

时间: 2024-08-29 11:39:38

golang 自定义importpath的相关文章

golang自定义数据类型查询与插入postgresql中point数据

golang自定义数据类型查询与插入postgresql中point数据 具体代码如下: package main import ( "bytes" "database/sql" "database/sql/driver" "fmt" _ "github.com/lib/pq" "strconv" "strings" ) // 自定义支持类型 type Point s

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

package protocol import ( "bytes" "encoding/binary" ) const ( ConstHeader = "jackluo" ConstHeaderLength = 7 ConstSaveDataLength = 4 ) //封包 func Packet(message []byte) []byte { return append(append([]byte(ConstHeader), IntToBy

Golang自定义包导入

# 文件Tree project -/bin -/pkg -/src -main.go -/test -test1.go -test2.go main.go package main import ( "fmt" "./test" ) func main() { fmt.Print("test1\n") test.Ojbk1() fmt.Print("test2\n") test.Ojbk2("okokok"

golang 自定义接口体(与其他语言对象类似)

1 /* 2 3 结构体变量: 4 结构体的定义只是一种内存布局的描述,只有当结构体实例化时,才会真正地分配内存, 5 因此必须在定义结构体并实例化后才能使用结构体的字段. 6 type 类型名 struct { 7 字段1 字段1类型 8 字段2 字段2类型 9 … 10 } 11 指针类型的结构体 :new 关键字对类型(包括结构体.整型.浮点数.字符串等)进行实例化,结构体在实例化后会形成指针类型的结构体 12 1 var o *类型名 = new(类型名) 13 2 o := &类型名{

golang 自定义接口 和 实现接口

1 /* 2 定义: 3 type 接口名 interface{ 4 方法名(可选:参数列表) 可选:返回值列表 || (可选:返回值列表) 5 } 6 例:type Writer interface { 7 Write(p []byte) (n int, err error) 8 } 9 type Objecter interface{//定义接口 10 say(class int, value string) (b bool, err error) 11 } 12 实现接口: 13 1:接口

go err

golang自定义err方案很多 // Errno 代表某种错误的类型 type Errno int func (e Errno) Error() string { return "errno " + strconv.Itoa(int(e)) } func main(){ // 示例3. const ( ERR0 = Errno(0) ERR1 = Errno(1) ERR2 = Errno(2) ) var myErr error = Errno(0) switch myErr {

Golang通过自定义函数实现模板的包含

Golang通过自定义函数实现模板的包含 Golang原生不支持例如revel中指令{{ template "header.html" }} ? 1 package main import ( ? 1 2 3 4 "html/template" "log" "os" "io/ioutil" ) func main() { ? 1 2 3 s := ParseTmplateToStr("src/1.

[Golang] 从零开始写Socket Server(2): 自定义通讯协议

在上一章我们做出来一个最基础的demo后,已经可以初步实现Server和Client之间的信息交流了~ 这一章我会介绍一下怎么在Server和Client之间实现一个简单的通讯协议,从而增强整个信息交流过程的稳定性. 在Server和client的交互过程中,有时候很难避免出现网络波动,而在通讯质量较差的时候,Client有可能无法将信息流一次性完整发送,最终传到Server上的信息很可能变为很多段. 如下图所示,本来应该是分条传输的json,结果因为一些原因连接在了一起,这时候就会出现问题啦,

【玩转Golang】 自定义json序列化对象时,非法字符错误原因

由于前台web页面传来的日期对象是这样的格式“2010-11-03 15:23:22”,所以我安装网上查来的办法,自定义包装了time.Time对象,实现自己的Marshal和UnMarshal方法 type DateTime struct { time.Time } const ctLayout = "2006-01-02 15:04:05" const ctLayout_nosec = "2006-01-02 15:04" const ctLayout_date