Golang build失败
使用go mod管理依赖时,build出现问题
复现
$ go build test.go
出现以下错误:
test.go:9:8: no required module provides package github.com/Unknwon/goconfig: working directory is not part of a module
test.go:10:8: no required module provides package gopkg.in/confluentinc/confluent-kafka-go.v1/kafka: working directory is not part of a module
原因
未初始化模块
解决方案
go mod init
go mod init example.com/m
go get github.com/Unknwon/goconfig
go build test.go
up@up123:~/scripts$ go mod init
go: cannot determine module path for source directory /home/up/scripts (outside GOPATH, module path must be specified)
Example usage:
'go mod init example.com/m' to initialize a v0 or v1 module
'go mod init example.com/m/v2' to initialize a v2 module
Run 'go help mod init' for more information.
up@up123:~/scripts$ go mod init example.com/m
go: creating new go.mod: module example.com/m
go: to add module requirements and sums:
go mod tidy
up@up123:~/scripts$ go build test.go
asset_traffic_producer.go:9:8: no required module provides package github.com/Unknwon/goconfig; to add it:
go get github.com/Unknwon/goconfig
asset_traffic_producer.go:10:8: no required module provides package gopkg.in/confluentinc/confluent-kafka-go.v1/kafka; to add it:
go get gopkg.in/confluentinc/confluent-kafka-go.v1/kafka
up@up123:~/scripts$ go get github.com/Unknwon/goconfig
go get: added github.com/Unknwon/goconfig v0.0.0-20200908083735-df7de6a44db8
up@up123:~/scripts$ go get gopkg.in/confluentinc/confluent-kafka-go.v1/kafka
go get: added github.com/confluentinc/confluent-kafka-go v1.6.1
go get: added gopkg.in/confluentinc/confluent-kafka-go.v1 v1.6.1
up@up123:~/scripts$ go build test.go