zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    uva-11361
    HDU
    LCS
    CodeForces
    linux 有趣的命令组合
    opencv识别封闭区域 并标记该区域
    宜出行人口热力图
    美团酒店
    赶集租房
    发送企业微信应用通知
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/9529994.html
Copyright © 2011-2022 走看看