I installed doozerd sources in:
home/stephan/src/go/src/pkg/github.com/ha/doozerd
$GOROOT = /home/stephan/src/go
$GOPATH = /home/stephan/src/go/src/pkg/
When I try to do ./all.sh in the doozerd checkout from github I get package could not be found locally errors.. :
.
imports github.com/ha/doozerd/peer
imports github.com/ha/doozerd/web
imports code.google.com/p/go.net/websocket: /home/stephan/src/go/src/pkg/github.com/ha/doozerd/web/web.go:4:2: package could not be found locally
.
imports github.com/ha/doozerd/peer
imports github.com/ha/doozerd/consensus
imports code.google.com/p/goprotobuf/proto: /home/stephan/src/go/src/pkg/github.com/ha/doozerd/server/conn.go:4:2: package could not be found locally
.
imports github.com/ha/doozer: /home/stephan/src/go/src/pkg/github.com/ha/doozerd/peer/peer.go:4:2: package could not be found locally
I thought go would find the missing requirements, and install them itself.
What am I missing?
$GOPATH =
/home/stephan/src/go/src/pkg/ means the go tool looks for packages in
/home/stephan/src/go/src/pkg/src/<import-path>, but you've put the sources in /home/stephan/src/go/src/pkg/github.com/ha/doozerd instead.
Related
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 am unable to load local package using go mod. I have seperate go.mod files for repoA and repoB. I have found no solution anywhere. OS is windows.
$> go version
go version go1.12.7 windows/amd64
I have two modules with repository, when I run main file from repoA. It will try to find module/package of repoB, then it throws an error saying
cannot find module providing package
My repo structure :-
����repoA
� ����proto
� � ����system
� ����sauth
� ����shandle
� ����smodel
� ����sresponse
����repoB
����common
����config
����proto
����account
����auth
����session
How does the go.mod file of repoA look?
Imports should always be absolute, e.g.:
import "github.com/Himanshu/repoB"
Then, in repoA's go.mod file you can add a replace directive to point "github.com/Himanshu/repoB" to a local path (could be relative).
Make sure to read the relevant parts of the Modules wiki and the official blog post on modules
After doing couple of things the issue got resolved with my IntelliJ.
Initialize the go module in the project and
Refer package with absolute path.
I have 2 folders -> repoA and repoB which are present in folder c:\goprograms. Run go mod init examplegoprogram.com/app. Now, I see go.mod file in goprograms folder with content module examplegoprogram.com/app. In repoA, you can now refer repoB as import "examplegoprogram.com/app/repoB"
If you are using new Intellij Ultimate, enable go modules under Project settings Alt + Ctrl + S Languages & Frameworks -> Go -> Go Modules
I'm trying to understand how to organize my golang project using go1.11 modules. I tried several options, but none of them worked.
I have some code in the main package under the application folder and a local package that the main package uses.
$GOPATH
+ src
+ application/
+ main/
+ main.go
+ otherFileUnderMainPackage.go
+ aLocalPackage/
+ someCode.go
+ someCode_test.go
+ someMoreCode.go
+ someMoreCode_test.go
Files in the main package, imports ../aLocalPackage. When I compile by go build main/*.go it's working.
Then, I ran go mod init application: V.0.9.9 and got the go.mod file, but the build always fails. I always get error about not finding the local package: build application:V0.9.9/main: cannot find module for path _/.../src/application/aLocalPackage. I also tried to place the local package right under src/, place it under main/ etc. but none of these methods worked for me.
What is the way to use modules and local packages?
Thanks.
Relative import paths are not supported in module mode. You will need to update your import statements to use a full (absolute) import path.
You should also choose a module name other than application. Your module path should generally begin with a URL prefix that you control — either your own domain name, or a unique path such as github.com/$USER/$REPO.
I had some problems working with local packages myself.
There are two tricks to make it work:
you run "go build" in the package directory
This compiles the package and places it in the build cache.
This link about code organisation in go explains more.
You can identify where the cache is using:
>go env GOCACHE
/home/<user>/.cache/go-build
Import using a path relative to the project
I puzzled loads over what the correct import path was and finally discovered that go doc or go list will tell you.
>go doc
package docs // import "tools/src/hello/docs"
>go list
tools/src/hello/docs
For example. I have a hello world API project and was using swaggo to generate documentation which it does in a docs sub-directory.
To use it I add an import:
_ "tools/src/hello/docs"
For my case the _ is important as docs is not used directly but we its init() function to be invoked.
Now in hello/main.go I can add "tools/src/hello/docs" and it will import the correct package.
The path is relative to the location of go.mod if you have one.
I have tools/ here as I have a go.mod declaring "modules tools".
Modules are a different kettle of fish - see https://github.com/golang/go/wiki/Modules.
Recent versions of go (1.11 and later) can create a go.mod file which you may use to fix the version of a module that is used and avoid go's crazy default behaviour of just downloading the latest version of any package you import.
I have written a blogpost on how to start your first Go project using modules.
https://marcofranssen.nl/start-on-your-first-golang-project/
In general it boils down to just create a new folder somewhere on your system (doesn't have to be in GOPATH).
mkdir my-project
cd my-project
go mod init github.com/you-user/my-project
This will create the go.mod file. Now you can simply create your project layout and start building whatever you like.
Maybe one of my other blogs can inspire you a bit more on how to do things.
https://marcofranssen.nl/categories/software-development/golang/
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
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.