登录
首页 >  Golang >  Go问答

类型与供应商文件夹不兼容

来源:stackoverflow

时间:2024-04-06 08:27:35 195浏览 收藏

本篇文章给大家分享《类型与供应商文件夹不兼容》,覆盖了Golang的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。

问题内容

我正在通过 jenkins 部署 go 应用程序并运行一些测试。

即使我从 gopath 中删除所有第三方库,我的所有测试也会在本地通过,因为我已通过 godep save 填充了我的 vendor 文件夹。

但是,当 jenkins 运行我的测试时,它报告 github 版本和供应商版本之间的类型不兼容:

mypackage/myfile_test.go:65:22: cannot use my_variable
(type "github.com/gocql/gocql".uuid) as type 
"myproject/vendor/github.com/gocql/gocql".uuid in assignment

我尝试使用 dep (go 团队的官方供应商经理)代替 godep,但它没有解决问题。

我是否需要告诉我的测试使用“myproject/vendor/github.com/gocql/gocql”而不是“github.com/gocql/gocql”? (更新:显然这是非法的,并且会给出错误 must be import as github.com/gocql/gocql。)

如何解决这个问题?

更新:

  • 我在本地计算机和 jenkins 服务器上都使用 go 1.12.1。
  • 我没有使用任何类型的 go 模块

这是我的 jenkins pipeline 代码的 go 部分。会不会和这个问题有关系?

steps {                                           
    // Create our project directory.
    sh 'cd ${GOPATH}/src'
    sh 'mkdir -p ${GOPATH}/src/myproject'

    // Copy all files in our Jenkins workspace to our project directory.                
    sh 'cp -r ${WORKSPACE}/* ${GOPATH}/src/myproject'

    // Copy all files in our "vendor" folder to our "src" folder.
    sh 'cp -r ${WORKSPACE}/vendor/* ${GOPATH}/src'

    // Build the app.
    sh 'go build'               

    // Remove cached test results.
    sh 'go clean -cache'

    // Run Unit Tests.
    sh 'go test ./... -v -short'    
}

解决方案


正如怀疑的那样,问题出在我的 jenkins 配置上(因为本地一切工作正常)。

事实证明,每一行 sh 都代表一个新的 shell 终端,因此我所需要做的就是将所有内容放入一个 sh 部分,如下所示:

steps {
    // Create our expected project directory inside our GOPATH's src folder.
    // Move our project codes (and its vendor folder) to that folder.
    // Build and run tests.
    sh '''                    
        mkdir -p ${GOPATH}/src/myproject
        mv ${WORKSPACE}/* ${GOPATH}/src/myproject
        cd ${GOPATH}/src/myproject
        go build
        go clean -cache
        go test ./... -v -short
       '''
}

非常感谢所有提供帮助的人!

理论要掌握,实操不能落!以上关于《类型与供应商文件夹不兼容》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>