-
创建新项目并用Golang IDE打开
mac下 Golang -> Perferences -> Go -> Go Modules(vgo) -> Enable Go Modules (vgo) integration打勾
结构及代码:
gosports/src/common/entity/test.go
package entity
type TestObject struct {
Id int64
Name string
}
gosports/src/common/gateway/main.go
package main
import (
"fmt"
"github.com/dylanpeng/gosports/src/common/entity"
"rsc.io/quote"
)
func main() {
test := entity.TestObject{ Id:1, Name:"test"}
fmt.Println(quote.Hello())
fmt.Println(test)
}
点击debug后,golang IDE自动添加go.mod文件
go.mod:
module github.com/dylanpeng/gosports
go 1.12
require rsc.io/quote v1.5.2 // indirect
以上是Golang IDE自动生成,go.mod自动在根目录~/gosports生成
-
想将go.mod放在其他位置应该如何?
本例将go.mod定位在src文件夹~/gosports/src下
手动生成go.mod
cd ~/gosports/src
go mod init gosports
然后修改golang设置
run -> Edit Configurations -> Working directory修改为gosports/src
此时module的根目录已经是gosports/src了,需要修改main中import
package main
import (
"fmt"
//这里修改module路径
"gosports/common/entity"
"rsc.io/quote"
)
func main() {
test := entity.TestObject{ Id:1, Name:"test"}
fmt.Println(quote.Hello())
fmt.Println(test)
}
改后直接启动即可