Hi everyone, I have a problem with importing package into main.go
I have made small demo that illustrate the issue.
Here is the folder structure:
YYY
| go.mod
| main.go
|
\---go
api_devices.go
I ran go mod init devices as well as go mod tidy but error remains: could not import devices/go/devicemanager (no required module provides package "devices/go/devicemanager").
Here are the relevant files, starting from api_devices.go:
I thought it should work, I tried Googling but found no solution that solves my case. I use VS Code in case it matters. Ask for additional info if needed.
Hello there. This error occurs, because conventionally folder should have the same name as the package inside this folder. Thus, if you want it to be called devicemanager rename the folder. As an alternative you can change the import statement to import devicemanager "devices/go", which works as well, but it’s not very common.
@lemarkar
Hi, thank you for trying to help.
I did try to rename folder into devicemanager and change import to "devices/devicemanager/devicemanager" but the same error stays.
Very strange and frustrating…
Try to imagine that the borders of your internal package are within the folder it’s located. And the import path is a related path, where devices is root. With double name like "devices/devicemanager/devicemanager" go will look for this folders structure
| devices
| - devicemanager
| - devicemanager // <- go is looking here, but you need to be 1 level above
The problem is not the package name (go is fine), but the module path:
The Go standard library uses package paths that do not contain a dot in the first path element, and the go command does not attempt to resolve such paths from network servers. The paths example and test are reserved for users […]
So, don’t use devices, but something with a dot in the first path element.
The Go standard library uses package paths that do not contain a dot in the first path element, […]
If you start your path without a dot in the first path element, your code could be considered part of the Go standard library.
So “devices” is reserved, “example.com” is not.
True. Still, there are a lot of tools, editors and other stuff involved. Do yourself favor and start at least with “example.com” or even better the repository you are experimenting in.
Never have I ever thought about name with or without a dot if it’s a part of the STD. The question here was not about naming convention, which is not even a convention, but because of the incorrect import statement. Dots, roots and everything else is okay if there is a plan to publish it somewhere, if not, there is no reasonable explanation to add dots or what is worse “example.com”
Agreed. And adding example.com won’t get you any closer to being able to publish this module (unless you own that domain!). For local dev where I’m doing something like building a Go app that is a one-time use thing (I use Go as a scripting language all the time) or just experimenting with new features like the JSON v2 package, I go mod init testjson or similar all the time.