go test error : import path contains backslash; use slash - go

I got the following output upon running test in my golang project (truncated. see the complete output down below) :
import path contains backslash; use slash: "gitlab.com\\group-name\\project-name/vendor/..."
project-name is the name of the project I'm working on. The project itself run smoothly, only the test was error.
I have no idea how such an import path (containing \\) generated, who could be responsible for generating that import (is it go test?), and how should I do about fixing it?
I tried to run the test after upgrading go version from 1.6.x to 1.8, if this matter.
environment :
go version go1.8 windows/amd64
goconvey v1.6.2
glide package manager
command :
go test api_test.go
output :
# command-line-arguments
.\api_test.go:6: import path contains backslash; use slash: "gitlab.com\\group-name\\project-name/vendor/github.com/smartystreets/assertions"
.\api_test.go:6: cannot import "gitlab.com\\group-name\\project-name/vendor/github.com/smartystreets/goconvey/convey"
due to version skew - reinstall package (bad package path "gitlab.com\\group-name\\project-name/vendor/github.com/smartystreets/assertions" for package assertions)
FAIL command-line-arguments [build failed]
api_test.go : (just a random sample of goconvey for now, and still produce the error)
package test
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestIntegerStuff(t *testing.T) {
Convey("Given some integer with a starting value", t, func() {
x := 1
Convey("When the integer is incremented", func() {
x++
Convey("The value should be greater by one", func() {
So(x, ShouldEqual, 2)
})
})
})
}

Related

Why `go test` complains undefined _test

I am new to Go. Why go test complains about the undefined test, while go test countdown_test.go main.go is fine?
❯ go test
# main.test
/var/folders/1z/p_x3q6b53mdf7_536x8_8h2h0000gn/T/go-build2418758029/b001/_testmain.go:13:2: cannot import "main"
/var/folders/1z/p_x3q6b53mdf7_536x8_8h2h0000gn/T/go-build2418758029/b001/_testmain.go:21:20: undefined: _test
FAIL main [build failed]
In the directory where I run the command has three files,
main.go:
package main
import (
"bytes"
"fmt"
)
func Countdown(out *bytes.Buffer) {
fmt.Fprint(out, "3")
}
countdown_test.go:
package main
import (
"bytes"
"testing"
)
func TestCountdown(t *testing.T) {
buffer := &bytes.Buffer{}
Countdown(buffer)
got := buffer.String()
want := "3"
if got != want {
t.Errorf("got %q want %q", got, want)
}
}
go.mod:
module main
go 1.17
Passed after changing the module name in go.mod file to:
module Countdown
go 1.17
Before change:
PS D:\##\##\##> go test
# main.test
C:\##\##\AppData\Local\Temp\go-build2311367811\b001\_testmain.go:13:2: cannot import "main"
C:\##\##\AppData\Local\Temp\go-build2311367811\b001\_testmain.go:21:20: undefined: _test
FAIL main [build failed]
After changing the module name:
PS D:\##\##\##> go test
PASS
ok Countdown 0.037s
Please check this refrence.
I will quote the important thing form it:
Even if you’re not at first intending to make your module available for use from other code, using its repository path is a best practice that will help you avoid having to rename the module if you publish it later.

go malformed import path empty path element

Trying to run
go mod init `pwd`
On a trivial example
package main
import (
"net/http"
"github.com/labstack/echo/v4"
)
func main() {
// Echo instance
e := echo.New()
// Route => handler
e.GET("/", func(c echo.Context) error {
return c.HTML(http.StatusOK, "Hello, World!\n")
})
// Start server
e.Logger.Fatal(e.Start(":1323"))
}
Gives an error
go malformed import path (path to file) empty path element
Yet if I create a manual go.mod like this
module <path>
go 1.12
require github.com/labstack/echo/v4 v4.1.6
Then I can build / run the code normally.
Any idea why go mod init fails?
Mostly for future reference as creating the go.mod solves the immediate issue.
Try initializing the modules in the console. The syntax for that is go mod init <mod_name> where mod_name can be github.com/labstack/echo/v4.
As suggested above, go mod init but without / at the beginning of your path works. Perhaps outside of GOPATH relative paths should be used instead of absolute paths(?)

Go local module import can't be resolved by IDE linter

The code compiles fine, but when using the replace directive for mapping package name to a local directory, the Go linter can't pick this up.
I've tried this on both VSCode & Goland, both has the lint error as shown below. The image is extra information, all code & error messages are shown below in text format.
Repository: https://github.com/webberwang/go-lint-error
This is the folder structure:
/core
/calc
math.go
go.mod
/main
app.go
go.mod
// main/app.go
package main
import (
"fmt"
"github.com/codelabstudios/core/calc" <- ERROR "Cannot resolve directory 'core'"
)
func main() {
result := calc.Add(1, 2) <- ERROR "Unresolved reference 'Add'"
fmt.Println("calc.Add(1, 2) => ", result)
}
// main/go.mod
module github.com/codelabstudios/main
go 1.14
require github.com/codelabstudios/core v0.0.0-00010101000000-000000000000
replace github.com/codelabstudios/core => ../core
// core/calc/math.go
package main
import (
"fmt"
"github.com/codelabstudios/core/calc"
)
func main() {
result := calc.Add(1, 2)
fmt.Println("calc.Add(1, 2) => ", result)
}
// core/calc/go.mod
module github.com/codelabstudios/core
go 1.14
After some digging, I found out that the "replace" directive is part of the Vgo proposal (the V stands for versioning). This was merged with Go in 1.11.
To fix the local module import error, we just need to enable "Vgo Integration" in the IDE.

Directory Structure and Import

I do not understand how the package / project directory structure works.
I am following these 2 links enter link description here and enter link description here
My Go workspace is located under /workspace/golang.
My $GOPATH is equal to /workspace/golang
My directory structure is as follow :
/workspace/golang/src/Tutorial/
...tutorial_main.go <- Default 'Hello World' program
...library/
......arithmetic.go
Content of arithmetic.go :
package library
func addNum(a int, b int) int {
return a + b
}
I cd into library folder and ran go build arithmetic
Now, I cannot figure out how to use my arithmetic.go in my tutorial_main.go file.
I tried the following :
import "library"
fmt.Println("Result : ", library.addNum(1,4))
import "Tutorial/library"
fmt.Println("Result : ", library.addNum(1,4))
import "src/Tutorial/library"
fmt.Println("Result : ", library.addNum(1,4))
Neither works.
It keep saying it cannot find library
I don't understand what I am doing wrong.
With your setup, the package import path is:
import "Tutorial/library"
And you should capitalize the names you want to export in the library package so you can access them from other packages.
In general, the import path is the file path of the package (relative to $GOPATH) if it is local, or the remote path of the package, such as github.com/myaccount/package. The simple import names such as import library are reserved for built-in packages. Relative import paths also work, but they are not recommended, i.e. import ./library.
That said, with the module system $GOPATH is no longer used. I recommend you read modules and how you can work outside the $GOPATH.
In Go, your variables and functions that you'd like to export (make available outside your package) need to start with a capital letter.
package library
func privateAddNum(a int, b int) int {
return a + b
}
func PublicAddNum(a int, b int) int {
return a + b
}
privateAddNum is an unexported function and will only be accessible within the library package.
PublicAddNum is an exported function and will be accessible to external packages that import library.

(import path does not begin with hostname)

My demo golang project is https://github.com/aQuaYi/demoGolangProjectWithCI
demoGolangProjectWithCI/subModel/subModelAdd.go is
package subModel
import (
"demoGolangProjectWithCI"
)
//Add returns sum of a and b
func Add(a, b int) int {
return demoGolangProjectWithCI.Add(a, b)
}
and my .travis.yml is
language: go
go:
- 1.8.3
script: go test ./...
but travis said me "package demoGolangProjectWithCI: unrecognized import path "demoGolangProjectWithCI" (import path does not begin with hostname)"
detail is https://travis-ci.org/aQuaYi/demoGolangProjectWithCI/builds/247416861
How Could I fix this?
Thank you very much.
demoGolangProjectWithCI is not resolved from subModel because import "demoGolangProjectWithCI" mean absolute path. You can write relative path like ./demoGolangProjectWithCI. But, in generally, you've better to write full-github-paths as github.com/aQuaYi/demoGolangProject since your package may be used by other one's project.

Resources