Skip to content

工程架构

golang工程架构建设,实现多模块打包

新建工程

创建目录

  • go-workspace
  • go-workspace\common
  • go-workspace\service

初始化各模块

进入 common service,分别执行:

shell
Go mod init mybatisx.top/Go-workspace/common
Go mod init mybatisx.top/Go-workspace/service

如需再次安装模块,执行:

shell
go mod tidy

工程整合

进入 go-workspace,执行:

shell
Go work init
Go work use ./common
Go work use ./service

测试

common工程下,新建add.go:

go
package common

func Add(x, y int) int {
	return x + y
}

service工程下,新建main.go:

go
package main

import (
	"fmt"
	"mybatisx.top/go-workspace/common"
)

func main() {

	add := common.Add(10, 20)
	fmt.Println("hello workspace, 调用common模块结果:", add)
}

下载

代码已上传gitee,下载地址:https://gitee.com/fly2world_admin/go-worksapce.git