douzhan6474 2019-04-16 09: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 06: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.

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

报告相同问题?

悬赏问题

  • ¥100 二维码被拦截如何处理
  • ¥15 怎么解决LogIn.vue中多出来的div
  • ¥15 优博讯dt50巴枪怎么提取镜像
  • ¥30 在CodBlock上用c++语言运行
  • ¥15 求C6748 IIC EEPROM程序固化烧写算法
  • ¥50 关于#php#的问题,请各位专家解答!
  • ¥15 python 3.8.0版本,安装官方库ibm_db遇到问题,提示找不到ibm_db模块。如何解决?
  • ¥15 TMUXHS4412如何防止静电,
  • ¥30 Metashape软件中如何将建模后的图像中的植被与庄稼点云删除
  • ¥20 机械振动学课后习题求解答
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部