Golang 1.11beta2 introduced experimental support for Modules.
I am failing to import a specific package from a go module.
This is the error when building the application:
$ go install
go: downloading github.com/udhos/modhello/modlib/lib v1.0.0
go: finding github.com/udhos/modhello latest
go: import "github.com/udhos/modhello/modapp" ->
import "github.com/udhos/modhello/modlib/lib": cannot find module providing package github.com/udhos/modhello/modlib/lib
Why is the import shown above failing?
This is the package 'lib' from module 'modlib':
# repo: modhello
# module: modlib
# package: lib
$ cat modhello/modlib/lib/modlib.go
package lib
func Sum(a, b int) int {
return a + b
}
$ cat modhello/modlib/go.mod
module github.com/udhos/modhello/modlib
This is the application 'modapp' :
$ cat modhello/modapp/main.go
package main
import (
"log"
"github.com/udhos/modhello/modlib/lib"
)
func main() {
run(1, 2)
}
func run(a, b int) {
log.Printf("Sum(%d,%d) = %d", a, b, lib.Sum(a, b))
}
$ cat modhello/modapp/go.mod
module github.com/udhos/modhello/modapp
require github.com/udhos/modhello/modlib v1.0.0
The git repository is tagged with 'modlib/v1.0.0'. This is how one publishes a version for a module.
Go version:
$ go version
go version go1.11beta2 linux/amd64
$ git --version
git version 2.18.0
I have posted this doubt also on golang-nuts: Host two distinct modules in one git repo?
go clean -modcache fixed the issue.
More details here: https://github.com/golang/go/issues/26695
Related
I am trying to link to some code online: https://github.com/TheCacophonyProject/audiobait
I am helping out with this project and have altered that repository to create a new package called audiofilelibrary.
I am then trying to use that code in a very simple program:
package main
import (
"fmt"
"github.com/TheCacophonyProject/audiobait/audiofilelibrary"
"github.com/TheCacophonyProject/audiobait/playlist"
)
func main() {
audio := audioFileLibrary.AudioFileLibrary{}
fmt.Println(audio.soundsDirectory)
sched := playlist.Schedule{}
fmt.Println(sched.Description)
}
You'll see I'm importing 2 packages from that repository, audiofilelibrary and playlist. playlist works, audiofilelibrary does not. They seem to be coded in the same way.
This is the error I get:
$ go build
go: finding github.com/TheCacophonyProject/audiobait v2.0.0
go: finding github.com/nathan-osman/go-sunrise latest
go: finding github.com/TheCacophonyProject/audiobait/audiofilelibrary latest
build audiobaitpackagetest: cannot load
github.com/TheCacophonyProject/audiobait/audiofilelibrary:
cannot find module providing package
github.com/TheCacophonyProject/audiobait/audiofilelibrary
And this is my go.mod file, which is in a directory called audiobaitpackagetest:
module audiobaitpackagetest
go 1.13
require github.com/TheCacophonyProject/audiobait v0.0.0-20191013210352-81b0afd9a085
I created the module with the command go mod init audiobaitpackagetest.
How can I see the audiofilelibrary package please? As in, how can I import it into other code?
The code for this question is all here, so it can be easily cloned and run: https://github.com/Davo36/audiobaitpackagetest
Any help much appreciated.
You are using version v2.0.0 of github.com/TheCacophonyProject/audiobait.
This version doesn't contain audiofilelibrary package. But the master branch contains it.
You can change your go.mod file like this to use the master branch.
module audiobaitpackagetest
go 1.13
require github.com/TheCacophonyProject/audiobait master
Then run this commands:
go mod tidy
go mod vendor # if you want to vendor it
I'm trying to import go module from subdirectory. My repository looks like
+$ tree .
.
└── bar
├── bar.go
└── go.mod
1 directory, 2 files
+$ cat bar/go.mod
module github.com/graywolf/foo/bar
go 1.13
+$ cat bar/bar.go
package bar
func Bar() string {
return "bar"
}
when pushed to github this source code runs fine
+$ cat github.go
package main
import (
"fmt"
"github.com/graywolf/foo/bar"
)
func main() {
fmt.Println(bar.Bar())
}
+$ go run github.go
go: finding github.com/graywolf/foo latest
go: finding github.com/graywolf/foo/bar latest
go: downloading github.com/graywolf/foo v0.0.0-20191019144834-ffb419608ae6
go: extracting github.com/graywolf/foo v0.0.0-20191019144834-ffb419608ae6
bar
However it stops working when I push it to different git hosting. I tweak the url
diff --git a/bar/go.mod b/bar/go.mod
index 7407848..6134a24 100644
--- a/bar/go.mod
+++ b/bar/go.mod
## -1,3 +1,3 ##
-module github.com/graywolf/foo/bar
+module git.sr.ht/~graywolf/foo/bar
go 1.13
commit and push it and try to run it
+$ cat sr.go
package main
import (
"fmt"
"git.sr.ht/~graywolf/foo/bar"
)
func main() {
fmt.Println(bar.Bar())
}
+$ go run sr.go
go: finding git.sr.ht/~graywolf/foo latest
go: downloading git.sr.ht/~graywolf/foo v0.0.0-20191019153505-33a4721605aa
go: extracting git.sr.ht/~graywolf/foo v0.0.0-20191019153505-33a4721605aa
build command-line-arguments: cannot load git.sr.ht/~graywolf/foo/bar: module git.sr.ht/~graywolf/foo#latest (v0.0.0-20191019153505-33a4721605aa) found, but does not contain package git.sr.ht/~graywolf/foo/bar
It looks like github and source hut provide different go-import meta tag, notice the missing .git for source hut.
+$ curl -sSf https://github.com/graywolf/foo?go-get=1 | grep -A1 go-import
<meta name="go-import" content="github.com/graywolf/foo git https://github.com/graywolf/foo.git">
+$ curl -sSf https://git.sr.ht/~graywolf/foo?go-get=1 | grep -A1 go-import
<meta name="go-import"
content="git.sr.ht/~graywolf/foo git https://git.sr.ht/~graywolf/foo">
When I edit the source code to use foo.git it does find the module, but provide different error message:
+$ go run sr.go
go: finding git.sr.ht/~graywolf/foo.git latest
go: finding git.sr.ht/~graywolf/foo.git/bar latest
go: downloading git.sr.ht/~graywolf/foo.git v0.0.0-20191019153505-33a4721605aa
go: downloading git.sr.ht/~graywolf/foo.git/bar v0.0.0-20191019153505-33a4721605aa
go: extracting git.sr.ht/~graywolf/foo.git v0.0.0-20191019153505-33a4721605aa
go: extracting git.sr.ht/~graywolf/foo.git/bar v0.0.0-20191019153505-33a4721605aa
go: git.sr.ht/~graywolf/foo.git/bar: git.sr.ht/~graywolf/foo.git/bar#v0.0.0-20191019153505-33a4721605aa: parsing go.mod:
module declares its path as: git.sr.ht/~graywolf/foo/bar
but was required as: git.sr.ht/~graywolf/foo.git/bar
Btw both (github and source hut) works fine when the go.mod is in the root of the repository, issue happens only when it is in a subdirectory.
I guess my question is what can I do about it. Did anyone run into something like this? Why doesn't go just clone the ~graywolf/foo and not check the bar subdirectory?
I'm following tutorials and I think I may have missed something.
I have a Go project located at:
/Users/just_me/development/testing/golang/example_server
The contents are:
main.go
package main
import "fmt"
func main() {
fmt.Println("hi world")
}
I have a ~/go directory.
go env shows:
GOPATH="/Users/just_me/go"
GOROOT="/usr/local/Cellar/go/1.12.9/libexec"
I installed the suggested packages in VSCode.
When I save my main.go I get:
Not able to determine import path of current package by using cwd: /Users/just_me/development/testing/golang/example_server and Go workspace:
/Users/just_me/development/testing/golang/example_server>
How do I fix this?
Since your package is outside of $GOPATH, you may need to create a module file.
You'll need to init your go module using
go mod init your.import/path
Change the import path to whatever you like.
This way you set the import path explicitly, which might help fix it.
The resulting go.mod file looks like this:
module your.import/path
go 1.14 // Your go version
So if the go.mod file is in the same directory as a main.go file, you can now import submodules from it:
E.g. main.go:
package main
import (
"your.import/path/somepackage" // Import package from a subdirectory. This only works if `go.mod` has been created as above
)
func main() {
somepackage.SomeMethod()
}
And in somepackage/whatever.go:
package somepackage
import "fmt"
func SomeMethod() {
fmt.Println("Success!")
}
if you are using vs code, check if the go and code runner extensions are enabled, if enabled, try disabling and enabling again, and if not, install and enable, and download all requested packages.
I am struggling with my travis-ci build.
Here the arborescence of my project:
- github.com
- src
- MyLib
- MyLibImpl.go
- main.go
The main.go is referencing MyLib like this
package main
import "fmt"
import MyLib "github.com/hako910/GolangTest/src/MyLib"
func main() {
fmt.Printf("%v", MyLib.HelloWorld())
}
It works great on my machine but fails in travis-ci with this message:
$ go get -t -v ./...
github.com/hako910/GolangTest (download)
package github.com/hako910/GolangTest/src/MyLib: /home/travis/gopath/src/github.com/hako910/GolangTest exists but /home/travis/gopath/src/github.com/hako910/GolangTest/.git does not - stale checkout?
The command "eval go get -t -v ./... " failed. Retrying, 2 of 3.
Here is my source code https://github.com/hako910/GolangTest and here is my travis-ci build https://travis-ci.org/hako910/GolangTest/jobs/348845103
What is wrong with my configuration?
I have a variable VERSION in a make file that sets the version for binary at compile time using -ldflags
VERSION = $(strip $(TIMESTAMP))
LDFLAGS = -ldflags "-X main.buildTime $(BUILD_TIME) -X main.buildNumber $(VERSION)"
Now I want to get the VERSION in a package which is not main and print it. I tried bunch of options, but not able to make it work.
My question is how can I get it in the package and then print it to client at run time, such as you are connected to app version 2.0..??
Directory structure:
- main.go
- test/
- test.go
test.go
package test
var Version = ""
main.go
package main
import (
"fmt"
"test"
)
func main() {
fmt.Println(test.Version)
}
Finally, run:
go run -ldflags="-X test.Version 2.0.0" main.go
Outputs:
> 2.0.0
Since we can specify import path, we can set the value of a string everywhere, not only in main.
From go 1.5 up, syntax is changed to importpath.name=string.