简体   繁体   中英

How to configure inherited packages in the go.mod file

My project has three modules, the file directory is (/main.go) (/b/b.go) (/a/a.go).and the go.mod as follows

<-- /main.go-->:
package main    
import (
    "b.com/b"
)
...
<-- /go.mod-->
module main
go 1.16
require b.com/b v0.0.0
replace b.com/b => ./b

<-- /b/go.mod-->
module main
go 1.16
require a.com/a v0.0.0
replace a.com/a => ../a

In the file (/b/go.mod), if I write:

require a.com/a v0.0.0
replace a.com/a => ../a

it will show in (/main.go) that "b.com/b" cannot be imported. If not, the program is normal,What's wrong here?

First, make your module at the top level:

go mod init stackoverflow.com/user/repo

Then make pack/pack.go :

package pack

const Pack = 1

Then make main.go :

package main
import "stackoverflow.com/user/repo/pack"

func main() {
   println(pack.Pack)
}

Then build:

go build

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