(import path does not begin with hostname) - go

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.

Related

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 code from a separate package/folder [duplicate]

This question already has an answer here:
Imported struct from other package is undefined
(1 answer)
Closed 4 years ago.
I have this directory layout:
/baba
biz.go # package baba
/hello
foo.go # package main
biz.go looks like this:
package baba
func Foodd(z int) int {
return z + 5
}
and foo.go looks like this:
package main
import (
"fmt"
"log"
)
func main() {
log.Fatal(Foodd(3))
}
currently this doesn't compile because Foodd is not recognized. How do I import the baba package in foo.go?
I assume if I compile like so, that it will pull in the right files:
go build foo.go
Or do I need to include the files in the baba package in the go build command? (I would hope not).
You need to import the baba package in order to use it from your main package. That will look something like:
package main
import (
"fmt"
"log"
"github.com/the1mills/myproject/baba"
)
func main() {
log.Fatal(baba.Foodd(3))
}
Imported packages are referred to by their package name, which is usually, but not always, the last element of the import path.
Usually, people let goimports handle finding the correct import path and automatically adding it. Your editor of choice probably has goimports integration.
Also see this answer for some other references and how to set up your directory structure.

Structuring local imports without GitHub in Golang

I'm building a simple app and after reading the doc on structuring go applications, I'm still confused.
I want this structure:
practice
models (packaged as models)
a
b
routers (packaged as routers)
a
b
app.go
Inside of app.go, I have the following:
package main
import (
"net/http"
// I have tried the following:
"practice/models/a"
"practice/models/b"
"practice/models"
"$GOPATH/practice/models/a"
"$GOPATH/practice/models/b"
"$GOPATH/practice/models"
...
)
func main() {
http.HandleFunc("/a", AHandler)
http.HandleFunc("/b", BHandler)
http.ListenAndServe(":8080", nil)
}
The A and B models look like this:
package models
import "net/http"
func AHandler(w http.ResponseWriter, r *http.Request) {
// code
}
Two questions:
What in the world is the right way to import these files? Do I really have to push them to github in order to be able to reference them? I understand the $GOPATH is the namespace for the entire go workspace on a local machine. My $GOPATH is set to include this directory.
Do I need to define a main method inside of these files? Can I just export a single function and have that be the handling function?
I have consulted the docs
See How to Write Go Code.
Use this directory structure:
- practice
- go.mod
- app.go
- models
- a.go
- b.go
- routers
- a.go
- b.go
where go.mod is created with the command go mod init practice where practice is the module path.
Import the packages as follows:
import (
"practice/routers"
"practice/models"
...
)
Use the imported packages like this:
func main() {
http.HandleFunc("/a", models.AHandler)
http.HandleFunc("/b", models.BHandler)
http.ListenAndServe(":8080", nil)
}
You do not need to push to github.com, even if you use github.com in the module path.
The main function in the main package is the entry point for the application. Do not define main functions in packages other than main.
What follows is the original answer based on GOPATH workspaces:
See How to Write Go Code.
Create your directory structure under $GOPATH/src.
$GOPATH
src
practice
models
routers
Import the packages as follows:
import (
"practice/routers"
"practice/models"
...
)
Use the imported packages like this:
func main() {
http.HandleFunc("/a", models.AHandler)
http.HandleFunc("/b", models.BHandler)
http.ListenAndServe(":8080", nil)
}
You do not need to push to github.com, even if you use 'github.com' in the file path.
The main function in the main package is the entry point for the application. Do not define main functions in packages other than main.
I think the other answer is out of date, you don't need to use GOPATH anymore.
Run:
go mod init yellow
Then create a file yellow.go:
package yellow
func Mix(s string) string {
return s + "Yellow"
}
Then create a file orange/orange.go:
package main
import "yellow"
func main() {
s := yellow.Mix("Red")
println(s)
}
Then build:
go build
https://golang.org/doc/code.html

go test error : import path contains backslash; use slash

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)
})
})
})
}

Is it possible don't specify package name?

Here is an example of my code:
package main
import (
"./bio"
)
func main() {
bio.PeptideEncoding(genome, codonTable)
}
Is it possible to use functions from my paxkage (bio) without specifying package name:
func main() {
PeptideEncoding(genome, codonTable)
}
?
You could use as an import declaration like:
. "./bio"
If an explicit period (.) appears instead of a name, all the package's exported identifiers declared in that package's package block will be declared in the importing source file's file block and must be accessed without a qualifier.
That is what a testing framework like govey does:
package package_name
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)
})
})
})
}
You don't need to use convey.So(), or convey.Convey() because of the import starting with a '.'.
Don't abuse it though, since, as twotwotwo comments, The style guide discourages it outside of tests.
Except for this one case, do not use import . in your programs.
It makes the programs much harder to read because it is unclear whether a name like Quux is a top-level identifier in the current package or in an imported package.
That is why I mentioned a testing framework using this technique.
As commented by Simon Whitehead, using relative import is not generally considered as the best practice (see for instance "Go language package structure").
You should also import the package via the GOPATH instead of relatively, as show in "Import and not used error".

Resources