Loading...
Loading...

Reputation: 913

How do I fix VS Code error: "Not able to determine import path of current package by using cwd" for Go project

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

I have a Go project located at:

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

The contents are: main.go

package main

import "fmt"

func main() {

    fmt.Println("hi world")
}

I have a ~/go directory.

go env shows:

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

I installed the suggested packages in VSCode.

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>

How do I fix this?

Upvotes: 4

Views: 7478

Answers (2)

user11352107
user11352107

Reputation:

if you are using vs code, check if the go and code runner extensions are enabled, if enabled, try disabling and enabling again, and if not, install and enable, and download all requested packages.

Upvotes: 2

xarantolus
xarantolus

Reputation: 2029

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

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.

The resulting go.mod file looks like this:

module your.import/path

go 1.14  // Your go version

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

E.g. 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()
}

And in somepackage/whatever.go:

package somepackage

import "fmt"

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

Upvotes: 4

Related Questions