Getting an error: cannot use *company/model as type *vendor/company/model
Flat Vendor structure : vendor/company/model
The files import "company/model"
You have a package company/model that is both vendored (under vendor/) and in your global $GOPATH, you additionally have an unvendored package that your package depends on that depends on company/model. So your package and the unvendored package are both trying to use the type company/modal but finding them in different places. The solution is to vendor the package that is not vendored.
delete the packages under the vendor directory
Follow GarMan's explaination, running command govendor add +e works.
Related
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
has anyone an idea to fix this problem?
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"
)
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
I have my projects that have many packages which import each other and import outside packages. When I make a change to one of my low lever packages, and then push it to git it is fine and works in that section. When I go get it for use in another project that was working perfectly I now get this go get this error:
module declares its path as: github.com/xdg-go/scram
but was required as: github.com/xdg/scram
None of my code uses either of those directly. It looks like it automatically updated some lower external packages and broke things the used to then old import.
How do I either find out the package that is importing the wrong name or stop all auto-updates?
The go.mod file at github.com/xdg/scram declares itself as github.com/xdg-go/scram:
module github.com/xdg-go/scram
go 1.11
require (
github.com/xdg-go/stringprep v1.0.2
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
)
The go.mod file should be updated to reflect the correct import path.
Unfortunately if this module is for you an indirect dependency, the best fix possible is to update whatever project you import that is directly importing it.
When that is not an option, a solution to this error is to clone the problematic repository locally and use the replace directive in your go.mod file:
module mymodule
replace github.com/xdg/stringprep => ../strprep
go 1.16.2
require (
github.com/divjotarora/mgo v0.0.0-20190308170442-1d451d2a3149
)
where ../strprep is where the code of the required module exists in your local machine, relative to the go.mod file of your project.
The downside of this of course is that you have to replicate this palliative fix wherever you plan to go get your modules.
Note also:
divjotarora/mgo is just a random example of a project that imports one of those packages using their old import path.
I'm using xdg/stringprep as an example because I can't find modules that import xdg/scram instead, but apparently it suffers from the same issue
Beside, you can use:
go mod why <package> to find out why a certain package is listed as a dependency of your project
go mod graph to show the full dependency graph. The output is in <package> <requirement> format
I have an log package in my project src folder. But when I include the log package from an another package as following, the go seams find the log in the system folder instead of my package.
import ("log")
And seams I cannot using relative path to import log package, because go install give following error:
local import "./log" in non-local package
So how can I let the go using my log package?
You need to add your package inside $GOPATH
So if your package is in
$GOPATH/src/github.com/ZijingWu/awesomeapp/src/
your log package would be in
$GOPATH/src/github.com/ZijingWu/awesomeapp/src/log
then it would be possible to use
import("github.com/ZijingWu/awesomeapp/src/log")
The paths seams a bit strange tho, and maybe you should consider adding the log package as a completely separate repository on github, so it would look something like.
import("github.com/ZijingWu/log")
Just using github as an example here, could of course be placed wherever, also just guessing at your username there. Doesnt need to be in github either you can just place it localy on your computer in the $GOPATH, but then noone else would be able to build your package.
I've my folder setup like this:
-src
--bitbucket.org
---eagleamulet
----myFirst.go (package main)
-----utils
------tempconv
-------tempconv.go (package tempconv)
However I'm not able to add the tempconv package to myFirst.go My Go environment settings look ok, so I'm not sure what's wrong here:
temppackage
goenv
Any pointers are greatly appreciated!
thanks
EA
Keep forgetting about qualifying the function names. It would have worked if I had done . to import into the current namespace.
All the packages imported are looked in under GOROOT and GOPATH environment variables first. Make sure your package is somewhere under these directories.
Now Suppose GOPATH is set to : /Users/test/Desktop/GoProject/src(lets assume, your src directory)
and GOROOT : /usr/local/go (where go is installed)
. If a file(myFirst.go) in your GoProject has a package imported as
import "abc/def/packageName"
then it should be present at any of the below two places:
/Users/test/Desktop/GoProject/src/abc/def/packageName/*
/usr/local/go/src/abc/def/packageName/*
If not, you will get import error.
The files inside these directories will have the first line as
package packageName
stating that all these files constitutes a package packageName
In my GOPATH I have something like this:
/bin/
/pkg/
/src/
/src/my_prog/
/src/my_prog/main.go
/src/my_prog/d_interface.go
/src/my_prog/d_struct_that_implements_the_interface.go
In main.go I have package main, in d_interface.go and d_struct_that_implements_the_interface.go I have package my_prog.
When I try to go build my_prog I get the following error:
can't load package: package my_prog: found packages my_prog (d_interface.go) and main (main.go) in C:\dev\Code\Go\src\my_prog
Does this mean that any file that belongs to package main should go in its own folder? If so, what is the reason for this?
Yes, each package must be defined in its own directory.
The source structure is defined in How to Write Go Code.
A package is a component that you can use in more than one program, that you can publish, import, get from an URL, etc. So it makes sense for it to have its own directory as much as a program can have a directory.
Also, if all you are trying to do is break up the main.go file into multiple files, then just name the other files "package main" as long as you only define the main function in one of those files, you are good to go.
Make sure that your package is installed in your $GOPATH directory or already inside your workspace/package.
For example: if your $GOPATH = "c:\go", make sure that the package inside C:\Go\src\pkgName