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?
Related
Hi could someone help me run my main.go: go run main.go ?
There are two folders, which are next to each other:
proj1 folder has main.go, go.mod
package1 folder has package1.go, go.mod, utility.go
inside of main.go:
package main
import (
"package1"
"fmt"
)
func main() {
y := package1.Struct1{
v: "1",
}
z := package1.isTrue()
fmt.Println(z)
}
inside my package folder: package1.go
package package1
type Package1 struct {}
func (a *Package1) IsTrue() bool {
return true
}
My Go version: go1.15.2 linux/amd64
My Go env settings:
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/user1/.cache/go-build"
GOENV="/home/user1/.config/go/env"
GOMODCACHE="/mnt/sda5/gopath/pkg/mod"
GOOS="linux"
GOPATH="/mnt/sda5/gopath"
GOROOT="/usr/local/go"
...
I tried :
go install, go build ... results no error inside the package folder
go mod vendor, go run main.go, go get -u package1 ... result in the same message when run inside the proj1 folder:
package package1 is not in GOROOT (/usr/local/go/src/package1)
The VS Code Go Plugin Linter shows no problem.
Please help. Thank you!
To solve the error i was facing package package1 is not in GOROOT (/usr/local/go/src/package1)
I had to ensure the environment variables were correctly configured.
I added those lines in the bashrc file:
export GO111MODULE=on
#GOPATH MUST BE OUTSIDE OF GOROOT directory!!!
export GOPATH=/mnt/sda1/programming/gopath
export PATH=$PATH:$GOPATH/bin
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
I loaded the bashrc file in the terminal:
source ~/.bashrc
Now i can execute following procedure to program with the Go language.
Make a new main folder...
Inside this main folder:
make main.go file begin with package main
Run the command below:
go mod init main
make another folder with the new package name: e.g. package1
inside the package1 folder:
make all files with package package1 in its 1st line ... but DO NOT MAKE MOD FILE inside this package folder!!!
in your main.go, you can import that package and use it
import "main/package1"
y := package1.Struct1{a: 1, b: 2,...}
z := y.func1()
This page is the top search result for...
package xxxxx is not in GOROOT (/usr/lib/go/src/xxxxx)
In my case, I was trying:
go build mything
That gave me the error. The solution was simple, I needed to be more specific:
go build mything.go
I faced the same problem but in my case i forgot the .go so after running :
problem : go run main
solution: go run main.go
and now it works fine
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?
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
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.
I have a project that provides a library (exports some funcs) and also must provide a command-line interface (there must be an executable file).
Example of directory structure:
whatever.io/
myproject/
main.go
myproject.go
The go compiler needs the package main and func main to start execution. My library needs the package myproject where I put stuff on it. This is what the go tool says when I am building another project that tries to import myproject:
main.go:5:2: found packages myproject (myproject.go) and main (main.go) in $GOPATH/src/whatever.io/myproject
So I believe there is no way to do it.
Should I move the library or the CLI to another package?
Just move your packages inside a new folder within the same directory of main.go.
Remember to import the new package from the reference of the $GOPATH.
Example:
user#user:~/p/go/test/so-multipack$ ls -R
.:
a main.go
./a:
a.go
user#user:~/p/go/test/so-multipack$ cat main.go
package main
import (
"../so-multipack/a"
)
func main(){
a.Hello()
}
user#user:~/p/go/test/so-multipack$ cat a/a.go
package a
import (
"fmt"
)
func Hello(){
fmt.Println("hello from a")
}
user#user:~/p/go/test/so-multipack$ go run main.go
hello from a
user#user:~/p/go/test/so-multipack$ go build
user#user:~/p/go/test/so-multipack$ ls
a main.go so-multipack
user#user:~/p/go/test/so-multipack$
Useful link:
go build vs go build file.go
You cannot have two packages per directory, hence the error. So the solution as #Larry Battle said to move your myproject.go to a new directory.
From How to write go code
Go code must be kept inside a workspace. A workspace is a directory
hierarchy with three directories at its root:
src contains Go source files organized into packages (one package per directory),
pkg contains package objects, and
bin contains executable commands.
In most cases, no. However, there is an exception for unit tests.
Working Example:
Here are 2 different packages (mypackage and mypackage_test) in 1 directory (mypackage). The compiler will not complain about this.
mypackage folder:
mypackage/
foo.go
foo_test.go
mypackage/foo.go:
package mypackage
func Add(a int, b int) int {
return a + b
}
mypackage/foo_test.go:
package mypackage_test
// Unit tests...
Rules:
The 2 packages must have the following names:
NameOfDirectory.
NameOfDirectory + _test.
The names of the files in the _test package must end with _test.go
If you're receiving a confusing compiler error along the lines of found packages "foo" and "bar", you've probably broken one or more of these rules.
You can't have two golang files in one directory with two packages. So you need to move main.go out of myproject.
the directory structure before move
whatever.io/
go.mod
myproject/
main.go
myproject.go
After move
whatever.io/
go.mod
main.go
myproject/
myproject.go
And you need to change your main.go's import path. If the module name is aaa
Before
import "aaa"
Need change to this
import "aaa/myproject"