如何修复VS Code错误:“无法通过使用cwd确定当前程序包的导入路径"用于围棋项目 [英] How do I fix VS Code error: "Not able to determine import path of current package by using cwd" for Go project

查看:209
本文介绍了如何修复VS Code错误:“无法通过使用cwd确定当前程序包的导入路径"用于围棋项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习教程,我想我可能错过了一些东西.

I'm following tutorials and I think I may have missed something.

我在以下位置有一个Go项目:

I have a Go project located at:

/Users/just_me/development/testing/golang/example_server

内容是: main.go

package main

import "fmt"

func main() {

    fmt.Println("hi world")
}

我有一个〜/go 目录.

转到环境显示:

GOPATH="/Users/just_me/go"
GOROOT="/usr/local/Cellar/go/1.12.9/libexec"

我在VSCode中安装了建议的软件包.

I installed the suggested packages in VSCode.

当我保存我的 main.go 时,我得到了:

When I save my main.go I get:

Not able to determine import path of current package by using cwd: /Users/just_me/development/testing/golang/example_server and Go workspace: 
/Users/just_me/development/testing/golang/example_server>

我该如何解决?

推荐答案

由于您的软件包位于 $ GOPATH 之外,因此您可能需要创建一个模块文件.

Since your package is outside of $GOPATH, you may need to create a module file.

您需要使用以下命令初始化go模块

You'll need to init your go module using

go mod init your.import/path

将导入路径更改为您喜欢的任何内容.这样,您可以显式设置导入路径,这可能有助于修复它.

Change the import path to whatever you like. This way you set the import path explicitly, which might help fix it.

生成的 go.mod 文件如下所示:

module your.import/path

go 1.14  // Your go version

因此,如果 go.mod 文件与 main.go 文件位于同一目录中,则现在可以从其中导入子模块:

So if the go.mod file is in the same directory as a main.go file, you can now import submodules from it:

例如 main.go :

package main

import (
   "your.import/path/somepackage" // Import package from a subdirectory. This only works if `go.mod` has been created as above
)

func main() {
    somepackage.SomeMethod()
}

并在 somepackage/whatever.go 中:

package somepackage

import "fmt"

func SomeMethod() {
    fmt.Println("Success!")
}

这篇关于如何修复VS Code错误:“无法通过使用cwd确定当前程序包的导入路径"用于围棋项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆