go新手问题,本地调包问题

使用Go Module 调用本地包

困扰新手许久的问题

调用非同一项目下的本地包

结构如下:

└── test1
    ├── test2
    │   ├── go.mod
    │   └── main.go
    └── test3
        ├── go.mod
        └── utils.go

调用本地包的测试

vim test1/test2/main.go
package main
import "test3"

func main() {
        test3.Test3Print()
}
vim test1/test3/utils.go
package test3

import "fmt"

func Test3Print() {
        fmt.Println("test3.Print")
}

对test2 和 test3 分别初始化

cd test1/test3
go mod init
cd ../test2
go mod init

不同项目情况下导入包需要在go.mod文件进行replace
使用go module导入本地包

test2/go.mod

module exercise/test1/test2

go 1.20

replace test3 => ../test3
$ go get test3
go: added test3 v0.0.0-00010101000000-000000000000

但在运行test2/main.go 依旧报错

$ go run main.go
main.go:3:8: package test3 is not in GOROOT (/home/xxx/go/go1.20.1/src/test3)

————问题已解决————

目前已知是workspace的问题

$GOPATH下进行

go work init

目前看到的配置gopath的资料都是单进行了初始化,但不知道什么原因在我的系统中仅做初始化workspace不会去搜索module

因此,在$GOPATH中需要额外进行

go work use .

即可正常使用go module

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值