`go get` command fails: unrecognized import path "_/<path>" - go

Basically, my question is, why is it putting an underscore in front of my import path?
It says import path does not begin with hostname which I'm assuming is because it starts with an underscore.
I read somewhere this may have something to do with me screwing up my GOPATH, but I've tried moving it everywhere, inside the project folder, outside the project folder, in the default location, etc.
I'm new to go and this has come up a few times recently. Would appreciate any guidance!

So I was misunderstanding where my source code had to be.
For anyone in the same boat, it needs to be within the go path in the actual src folder.
I found this helpful.

Related

Replacing inconvenient package name. Got error: replacement module without version must be directory path (rooted or starting with ./ or ../)

Problem
In go.mod file I wrote:
module github.com/Siiir/vector
go 1.17
require github.com/huandu/go-clone v1.3.2 // indirect
replace clone => github.com/huandu/go-clone[v1.3.2]
It says that I cannot do such a replacement.
I actually solved my problem with the name of imported package.
It is convenient & working without that dash. I found that I can use clone.something to refer to a function.
No need to type go-clone.something.
Anyway, assume that a package name is indeed crazy or inconvenient. How can I replace it?
What I've seen:
I've seen a sibling question:
go modules - replace does not work - replacement module without version must be directory path (rooted or starting with
What I tried:
Working with terminal:
go mod edit -replace=clone=github.com/huandu/go-clone
got: go: -replace=clone=github: unversioned new path must be local directory
manual editing:
Attempts like: replace clone => github.com/huandu/go-clone[v1.3.2]
got: replacement module without version must be directory path (rooted or starting with ./ or ../)
Anyway, assume that a package name is indeed crazy or inconvenient. How can I replace it?
You cannot.
And you should not. The import path is something you write just once in the import declaration and the package name can be changed on a per file level with import nicename "something.you.think/is-totally/inconvenient/and/unacceptable-to/your_taste" .

how to fix directory for import?

so basically i am trying a change prefix command and i want to fix the directory
i am trying to do import cogs.json in this
file directory
I realize that i get the errorModuleNotFoundError: No module named 'cogs' because of the directory of files but i dont know how to fix the directory to go there
can anyone help?
`i am trying to go to the _json.py inside of the cogs folder, besides that i might try just moving the _json file into the bot folder if that might work
Use
from cogs import _json
also, if you want you can rename your local name for the package
from cogs import _json as jsonModule
if this doesn't work, try
from ..cogs import _json
For more help, please look at this https://docs.python.org/3/reference/import.html

Go install exclude file

I have created a go script that compiles, starts, checks the status, and ends a web service I created (that is also in go). However, I have come to a road block.
With the compile feature I run the following command:
go install .
Which gives the following error:
./script.go:55: main redeclared in this block
previous declaration at ./hello.go:8
Which makes sense as I have two different files, both with the main func and main package. I also tried moving the script to another folder and then changing the command ran to:
go install {path}
Where {path} is equal to the path I want installed/compiled. Which I then got the following error:
exit status 1: can't load package: package /var/www/test.com/go: import "/var/www/test.com/go": cannot import absolute path
So in conclusion I have thought of only one solution (and I am up to hear others if mine isn't the best approach). My idea is to exclude the script file from compiling with the rest of the files, but I am unsure how to.
I did some research and couldn't find an easy way to do it (such as an --exclude flag with the go install command). Does anybody know how to accomplish what I am trying to achieve?
Thank you.
you could give the hello.go a different package name, that should work. Or i am missing something?
Regards
Tim

sphinx-autogen unable to find module

I'm trying to implement one of the answers to this question. However, I haven't been successful because when I run
> sphinx-autogen -o generated *.rst
I get the errors
Failed to import 'MyMod.X': no module named MyMod.X
Failed to import 'MyMod.Y': no module named MyMod.Y
Failed to import 'MyMod.Z': no module named MyMod.Z
Within my .rst files, there is one with the line:
.. automodule:: MyMod.X
(and similarly for MyMod.Y and MyMod.Z).
I'm running this within a subdirectory docs. In the parent directory containing docs, there is also a subdirectory MyMod which contains __init__.py, X.py, Y.py, and Z.py. The conf.py file within docs has the line sys.path.insert(0, os.path.abspath('../')) immediately after import sys.
The closest related question I can find is this, but the answers there seem to suggest that it is solved by inserting '../' into the path, which I had already done. Also, sphinx-autobuild can find these modules happily, so I don't think this is the issue.
Interestingly, changing the line in my .rst file to be .. automodule:: ../MyMod.X gets rid of the error message, though nothing seems to be generated in the directory I expect, and I then get error messages in sphinx-autobuild.
How can I get sphinx-autogen to read in these modules?
if anyone else has a better answer, or an explanation for this please post it, but here is what I learned about my question
Although sphinx-autogen gives error messages, in the case that I was looking at, the files I was trying to get it to create were still created. While it could not find the modules (and indeed, they were functions, not modules, so it's not a surprise that it couldn't), it still produced the expected output.

Q: Getting Build Error "Invalid Import Path"

I'm stuck on running the BeeGO app using "bee run" it says
The thing is I've already have setup properly my GOPATH to D:/Web Dev/GO/BeeGO/test-project/
and also routers path does exist and I've tried to manual build the file but it doesn't generate an .exe file.
Anyone knows how to fix this?
I'm using Windows 8.1 Pro (64-bit)
Thanks
GO expects the directory structure under $GOPATH in following ways as described in code organization:
$GOPATH/src <--- where your source code goes
/pkg
/bin
Instead of placing your source files directly under $GOPATH (D:/Web Dev/GO/BeeGO/test-project/ for your case), you want to move your code under $GOPATH/src.
D:/Web Dev/GO/BeeGO/test-project/src/main.go
D:/Web Dev/GO/BeeGO/test-project/src/quickstart/routers/routers.go
D:/Web Dev/GO/BeeGO/test-project/src/quickstart/controllers/controllers.go
import path should be always starting from $GOPATH/src. routers.go can be always imported as import "quickstart/routers" and controllers.go can be imported as import "quickstart/controllers".
That's not how you import a package.
The import path is relative to $GOPATH/src. use:
import "quickstart/routers"
Finally fixed the bug from the framework,
What I did:
in main.go import from
"D:/Web Dev/GO/BeeGO/test-project/quickstart/routers"
I changed it to _"../quickstart/routers" make sure to include _ this means to import the library even if it is not used,
Then in the routers/router.go I changed the import path
"D:/Web Dev/GO/BeeGO/test-project/quickstart/controllers" to "../controllers"
It seems BeeGO doesn't generate the template properly and caused the build to fail.
Another possiblity for this error, is when you copy-paste code from the internet,
and
import "quickstart/routers"
became
import "quickstart/routers "
due to bugs in some CMS/Blog systems (notice the space at the end before the closing quote...).

Resources