简体   繁体   中英

How to provide version number for go local packages to mention in `go.mod` file

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM