Q: Getting Build Error "Invalid Import Path" - go

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...).

Related

Universal scope and importing variables from one file to another

I am new at Golang. I am learning about universal scope and importing variables from one file to another.
Conditions:
System: Windows 11 x64
IDE: VS code
Language: GoLang
According to the universal scope I should be able to import variables from one file to another easily. Primary file: "main.go" and secondary file ""uniscope.go
Importing from uniscope.go into main.go
If the files are kept in the same folder directory then I can easily import using command: "go run main.go uniscope.go"
This runs without any error but until this command I get the error in main.go, "undeclared name", that is still not the main issue.
When I move the uniscope.go to another folder and then I run "go run main.go uniscope.go", it doesn't work, which is understandable as directory has been changed. So then I copy the path and paste it in import which in turns resolves the previous error, change the command to "go run main.go" but it throws out a new one.
"main.go:6:7: illegal character U+0073 's' in escape sequence"
I am not sure how I am wrong here. The video I am learning from is: https://www.youtube.com/watch?v=vYD9XWi_Xw8&list=PLve39GJ2D71xX0Ham0WoPaYfl8oTzZfN6&index=5
He is using itelliJ but I don't think that should be an issue.
I have tried the mentioned above and also tried finding the exact error on google. Just found a similar question posted on some Portuguese forum with no answers.
main.go:
package main
import "hello/world"
func main() {
println(world.Value)
}
world/world.go:
package world
const Value = 1
and run:
go mod init hello
go run .

Cannot import package "google.golang.org/api/compute/v1"

I am using cloud function to auto create some VM instance. Before, I was success in my use-case. You can view it here.Now, I want to update some line in metadata script. But when I save the code, it tell me some package I was import cannot find. Logs is
Build failed: src/cloudfunctions/function.go:9:2: cannot find package "google.golang.org/api/compute/v1" in any of:
/usr/local/go/src/google.golang.org/api/compute/v1 (from $GOROOT)
/workspace/src/google.golang.org/api/compute/v1 (from $GOPATH); Error ID: 2f5e35a0
But I was view document in https://pkg.go.dev/google.golang.org/api/compute/v1, usage example import like this import "google.golang.org/api/compute/v1", as same as me.
So, who can tell me what is $GOROOT and $GOPATH in cloud function, and how can I import this package.
Thanks in advance.
Run
go get -u -x google.golang.org/api/compute/v1
Then cd/open your directory in terminal then run
go mod init filename.go
then
go mod tidy
Also restart your IDE as that will need to update

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 get` command fails: unrecognized import path "_/<path>"

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.

Internal packages in Go

How to import internals packages in Go ?
import (
"runtime/internal/atomic"
"runtime/internal/sys"
)
Like this without get a error:
imports runtime/internal/atomic: use of internal package not allowed
And use internal funcs in a main package ?
Background
Go encourages structuring a program as a collection of packages interacting using exported APIs. However, all packages can be imported. This creates a tension when implementing a library or command: it may grow large enough to structure as multiple packages, but splitting it would export the API used in those additional packages to the world. Being able to create packages with restricted visibility would eliminate this tension.
A rule proposed to Go 1.4
An import of a path containing the element “internal” is disallowed if the importing code is outside the tree rooted at the parent of the “internal” directory.
Short answer
You can't (at least easily) and you shouldn't.
I will show you how I use internal nettest package:
// I copied nettest to vendor with `dep ensure` I think. Then:
mkdir vendor-local
cp -r vendor/golang.org/x/net/internal/nettest ./vendor-local/nettest
vim ./vendor-local/nettest/stack.go and remove import comment // import "foo" [1]
// Use full import in your go file:
import "github.com/foo-user/bar-project/vendor-local/nettest"
[1]: https://github.com/golang/net/blob/a8b9294777976932365dabb6640cf1468d95c70f/internal/nettest/stack.go#L6
Docs about import comments
You may find all import comments in your internal package with grep -r "// import" ./vendor-local/nettest
Why can't I copy nettest to ./vendor and use shorter import
You can, but utils like dep ensure that don't support local packages will purge your copy.

Resources