douzhan6474 2019-04-16 17:36
浏览 91
已采纳

通过Visual Studio Code进行的GoLang扩展:导入无效。 可以从常规命令行进行构建和测试

I am testing this very simple Go code on MacOS using VS Code. The project consists of these sample packages / files:

  1. azure.com/myproj/cmd/service/main/main.go
  2. azure.com/myproj/cmd/service/service.go
  3. azure.com/myproj/cmd/service/tests/test.go
  4. azure.com/myproj/internal/common/common.go

On the terminal command line, everything builds and all tests pass: go build . // (works in every folder) go test . // (tests work and pass)

However, from VS code I have 2 problems: 1. Imports from package to package do not work at all. For example:

package test

import (
    service "azure.com/myproj/cmd/service"    // VS complains on this line when running the test.
)
  1. The command that VS code runs is not just "go test . ". It passes extra parameters that include what seems to be a cached path, which I tried deleting from the file system, but it did not have an effect. This is what VS.code's Output tab contains:

Go Tests tab:

unknown import path "azure.com/myproj/cmd/service": 
cannot find module providing package 
azure.com/myproj/cmd/service

Go tab:

/Users/computername/go/src/azure.com/projname/cmd/service/tests 
>Finished running tool: /usr/local/bin/go test -c -o 
/var/folders/q5/hm9v_6x53lj0gj02yxqtkmd40000gn/T/vscode-goKGOMES/go- code-check azure.com/myproj/cmd/service/tests
can't load package: package 

azure.com/myproj/cmd/service: unknown import path 
"azure.com/myproj/cmd/service": cannot find module 
providing package azure.com/myproj/cmd/service

I do not understand what VS code is doing above with hm9v_6x53lj0gj02yxqtkmd40000gn and how I can change it. It looks like a cache.

So to summarize: When testing through VS Code, i do not understand why it's using the command that it is to run the tests (above), and why it can not find the imports, which the regular "go build . " and "go test . " commands have no problem with through the terminal.

Once again: From the terminal command line everything builds and all tests pass.

Seems to clearly be a VS Code related issue.

  • 写回答

1条回答 默认 最新

  • dongzhi8984 2019-04-17 14:07
    关注

    I found the answer to my problem. VS did not like the file path structure for the project. Here are the changes I made:

    1. Moved main.go from azure.com/myproj/cmd/service/main/main.go up a level to azure.com/myproj/cmd/service/main.go

    2. Moved the service implementation (service.go) into the internal folder: azure.com/myproj/internal/service/service.go

    3. Moved the tests into the internal folder: azure.com/myproj/internal/service/test/service_test.go

    How VS code is able to run the tests without complaining of the imports not being found.

    So now I only have the main.go in my /cmd/service folder. Everything lives in Internal, including tests. I suspect that burying main.go into a subfolder is what was confusing VS Code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛