• 傻瓜式的go modules的讲解和代码,及gomod能不能引入另一个gomod和gomod的use of internal package xxxx not allowed


    国内关于gomod的文章,哪怕是使用了百度 -csdn,依然全是理论,虽然golang的使用者大多是大神但是也有像我这样的的弱鸡是不是?

    所以,我就写个傻瓜式教程了。

    github地址:https://github.com/247292980/go_moudules_demo

    代码很少很简单。。。。

    环境变量 GO111MODULE,有三个值on,off,auto,很好理解,不配置的话默认是auto

    步骤

    1.新建文件夹 go_moudules_demo

    2.go mod之,生成go.mod文件

    go mod init go_moudules_demo
    语法
    go mod init [module]

    3.创建main.go,默认包名是gomod,需要改成main

    4.创建正真的存放代码的文件夹 demo和文件gomod.go,注意不能与main放在同一文件夹下,因为会造成包名冲突

     5.根据规则引入代码,这里有个坑,因为goland做的不太好,实际上golang的所有工具都做的不太好,导致代码报红,但是实际上go build/run还是能跑通的

     

    当然goland也可以配置

    总结

    gomod最容易让人进了误区就是,把自己之前的代码都gomod一次,那么后面使用的时候直接根据gomod的package找之前的代码,简直美滋滋。

    毕竟是go moudules但是,实际上只是go moudule,他只管一个项目里的多个包。

    为什么造成这个误区呢?因为国内说的都是包管理,我还真以为是针对包的操作,然后第一次尝试失败后,翻了下官网

    A module is a collection of related Go packages. 
    Modules are the unit of source code interchange and versioning.
    The go command has direct support for working with modules, including recording and resolving dependencies on other modules.
    Modules replace the old GOPATH-based approach to specifying which source files are used in a given build.

     a collection of related Go packages. 相关Go包的集合,这玩意的理解真的是难,什么相关,相关的是什么?这时候根据官网的usage代码反向理解下go mod init [module],显然是 module的相关Go包的集合,而module是一个单数啊。。。module和go mudules。。。我该如何理解啊。。。模板我倒是知道。。。总感觉这个怪不到谷歌头上,而且这玩意大家试个两下,就能找到正确理解也不算什么事。而且我要是把自己的代码都丢到github上同样不会报错,只是我是想着不丢到github上面的使用所以进了歪路。

    而第二句Modules are the unit of source code interchange and versioning. Modules是源码的版本控制和交换的单位,也就说明go mod之间是独立的,,,不能互调,除非在gopath里面。感觉大神看到这句两下都不用试了。。。

    四 语法解析

    主要是一个人的博客 http://blog.51cto.com/qiangmzsx/2164520?source=dra

    我把其中的关键抽出来,去掉他的代码,有兴趣的可以去原文看看

        go mod init:初始化modules
        go mod download:下载modules到本地cache
        go mod edit:编辑go.mod文件,选项有-json、-require和-exclude,可以使用帮助go help mod edit
        go mod graph:以文本模式打印模块需求图
        go mod tidy:检查,删除错误或者不使用的modules,下载没download的package
        go mod vendor:生成vendor目录
        go mod verify:验证依赖是否正确
        go mod why:查找依赖
    
        go test    执行一下,自动导包
    
        go list -m  主模块的打印路径
        go list -m -f={{.Dir}}  print主模块的根目录
        go list -m all  查看当前的依赖和版本信息

     五 gomod import另一个gomod

    似乎只能push到github或者使用类似gopath的方式,看了一圈没有说gomod能import本地的gomod。

    六 gomod遇到内部包的时候,报use of internal package xxxx not allowed

    https://github.com/golang/go/issues/26446 解决方法。

    同样gay网大神指出

    github.com/garyburd/redigo is not allowed to access github.com/gomodule/redigo/internal 
    because github.com/gomodule/redigo is not a prefix of github.com/garyburd/redigo:this is working as designed. If you intend for github.com/garyburd/redigo to replace github.com/gomodule/redigo, you'll need to use a replace directive and import via the latter path.

    也就是说要使用go mod edit -replace

    go mod edit -replace=github.com/garyburd/redigo@v2.0.0=github.com/gomodule/redigo@v2.0.0

    但是此时会报

    go: github.com/gomodule/redigo@v2.0.0+incompatible used for two different module paths (github.com/garyburd/redigo and github.com/gomodule/redigo)

    说真的我很想打人地说,又是你说要改成replace但是你现在只是换了个报错,这么敷衍的吗?

    然后,我想了想,换个版本号?

    go mod edit -replace=github.com/garyburd/redigo@v2.0.0=github.com/gomodule/redigo@v1.6.0

    程序成功运行!

    这时候我发现了一句话

    I'm planning to fix this before the final 1.11 release, but it might not make beta2.

    go的大神说在1.11的最新版本之前会修复,但不在beta2(当前),此时我看到是9天前,而这bug整了我至少3天,,,

    第一次追上大神的开发脚印?可喜可贺可喜可贺啊

    七 go proxy

    GOPROXY=https://goproxy.io

    gomodules配上goproxy就可以免翻墙拉大多数包了,看名字很多人觉得就是proxy而已,但是,在go2.0的计划中,https://goproxy.io十有八九要变成像maven那样的东西,因为只有这样才能更好的和gomodules配合做包管理工具

  • 相关阅读:
    热修复之类加载机制总结
    socket之tcp如何维护长连接
    sqlite之多线程处理
    android主线程ActivityThread-转载
    线程之ThreadLocal使用
    图片之压缩总结
    线程之交替执行的实例
    git的最常用命令总结
    sqlite之常见的语句
    activity之分析-3分钟看懂Activity启动流程
  • 原文地址:https://www.cnblogs.com/ydymz/p/9788804.html
Copyright © 2020-2023  润新知