3

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
Arun
  • 1,651
  • 4
  • 20
  • 31
  • You might want to take a look at [this](https://stackoverflow.com/questions/52026284/accessing-local-packages-within-a-go-module-go-1-11) – poWar Jun 11 '20 at 10:28
  • 1
    `import "local_pkg"` 100% wrong. If that package lives in module is github.com/kcarun/local_pkg you must import it like that. Note that the replace directive is wrong too: Import path do not end in /. Please read How to Write Go Code and stick to it. – Volker Jun 11 '20 at 12:09
  • 1
    go mod is supposed to work with version control via git, this feature of using a local package is mostly for dev but when your module works properly just push it and use version information from git – Shubham Srivastava Jun 11 '20 at 12:10
  • Hi @Volker as you suggested, I modified the code , but still not working. See the [Error](https://i.imgur.com/Pk8CYcw.png) – Arun Jun 12 '20 at 11:39
  • @Volker , Okay I started reading that document !!, – Arun Jun 12 '20 at 15:36

1 Answers1

1

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()
}
kozmo
  • 4,024
  • 3
  • 30
  • 48
  • I tried with this suggested modification , but still getting error [https://i.imgur.com/Pk8CYcw.png] – Arun Jun 12 '20 at 11:26
  • @Arun - fixed => try. – kozmo Jun 12 '20 at 15:43
  • It worked , there was a mistake from my side too, In local system my username is `arun` and in github it is `kcarun` this made some path issues in `go.mod` file and in my import statements – Arun Jun 13 '20 at 02:43