Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 353
Loading... Avatar asked Oct 27 '25 04:10

Loading...


1 Answers

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!")
}
like image 157
xarantolus Avatar answered Oct 29 '25 19:10

xarantolus