golang import引用项目下其他文件内函数

初始化项目

go mod init [module名字]
go mod init project

项目结构

go mod 文件

代码

需要暴露给外界使用的变量/函数名必须大写

main.go中引入,当前项目模块名/要引用的包名

go 复制代码
package main

import (
	// 这里的路径开头为项目go.mod中的module
	"project/initialize"
)

func main() {
	initialize.Routers()
}
// 在initialize的server.go中
package initialize

import (
	"fmt"

	"github.com/gin-gonic/gin"
)

func Routers() *gin.Engine {
	Router := gin.Default()
	fmt.Println("初始化路由")
	Router.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})
	Router.Run("127.0.0.1:8080")
	return Router
}
相关推荐
mldong20 小时前
工作流引擎的灵魂:状态机与 submitType
后端
BUG研究员_1 天前
Runnable与LCEL
开发语言·人工智能·python
朦胧之1 天前
Go 基础
后端·go
lun1 天前
Agent 工具调用成长史:理清楚 Function Calling、MCP、CLI 的关系
后端
Python私教1 天前
Codex 自动写 Commit 够安全吗?一套证据化 Git 提交流程
人工智能·git·后端
Python私教1 天前
AI Agent 上生产要不要开写权限?我把执行链拆成 4 道闸门
人工智能·后端·python
lun1 天前
Claude Code 多 Agent 实现:subAgent & Agent Teams
后端
牛艺翔1 天前
C++基础
开发语言·c++
键盘会跳舞1 天前
C++ :容器适配器stack源码级拆解
开发语言·c++··stack·先进后出
美味蛋炒饭.1 天前
Git 版本控制(下)
开发语言·git·学习·总结·后端开发