Home > Mobile >  GO - Could not import packages
GO - Could not import packages

Time:08-29

I use Go 1.19 and VScode 1.69.2

I get always this error message by importing a package from a second go file:

> import packages/calculator (no required module provides package
> "packages/calculator")

I set my GOPATH to my workingspace and my GOROOT is set to usr/local/go

Picture 1

Picture 2

Picture 3

Picture 4

Picture 5

has anyone an idea to fix this problem?

CodePudding user response:

Use modules to manage dependencies. Official docs: https://go.dev/blog/using-go-modules

Example:

go mod init project-name


go mod init example.com/project-name


go mod init github.com/you-user-name/project-name

*You may need to use the tidy command to clean up after running one of the above commands.

go mod tidy

Use the path format from above when importing a package into your go file

Example:

import (
   // Import internal and external packages like this
   "github.com/you-user-name/project-name/package-name"

   // Import standard library packages the normal way
   "testing"
   "math/rand"
)

CodePudding user response:

Just open your Go folder in VSCode instead of opening the folder that's one above. VSCode expects the root folder to be the one that contains go.mod. Also include the root folder of your project in your GOPATH

  • Related