Reputation: 1763
My intention is to mention local package in go.mod file, but stuck in package version part. (Go version is go1.14.4 linux/amd64
)
Error:
arun@debian:~/experiments$ go build
go: errors parsing go.mod:
/home/arun/experiments/go.mod:8: usage: require module/path v1.2.3
If blindly give a version number (for example : github.com/kcarun/local_pkg/app v1.2.3
in go.mod
, it gives unknown version error) while executing go build
go.mod
:
module github.com/kcarun/gitlandfill
go 1.14
replace github.com/kcarun/local_pkg/ => /home/arun/experiments/local_pkg/
require (
github.com/kcarun/local_pkg/app
)
main.go
:
package main
import "fmt"
import "local_pkg"
func main(){
fmt.Println("Ok")
app.SayHello()
}
app.go
:
package app
import "fmt"
func SayHello(){
fmt.Println("Is working!!")
}
Directory Structure :
arun@debian:~/experiments$ pwd
/home/arun/experiments
arun@debian:~/experiments$ tree
.
|-- go.mod
|-- local_pkg
| `-- app.go
`-- main.go
Upvotes: 3
Views: 696
Reputation: 4471
Right way to import "local" package as
.
├── go.mod
├── local_pkg
│ └── app.go
└── main.go
is
package main
import "fmt"
import "github.com/kcarun/gitlandfill/local_pkg"
func main(){
fmt.Println("Ok")
local_pkg.SayHello()
}
, without declared it in go.mod
:
module github.com/kcarun/gitlandfill
go 1.14
If the package
declared different than directory name (for example: dir is /home/arun/experiments/local_pkg
and pages is app
), you should import package use directory name, but to call it use package name:
package main
import "fmt"
import "github.com/kcarun/gitlandfill/local_pkg"
func main(){
fmt.Println("Ok")
app.SayHello()
}
Upvotes: 1