#!/bin/bash # Gomog 代码检查和编译脚本 set -e echo "======================================" echo " Gomog 代码检查" echo "======================================" cd "$(dirname "$0")" # 检查 Go 是否安装 if ! command -v go &> /dev/null; then echo "❌ 未找到 Go,请先安装 Go 1.21+" exit 1 fi echo "✓ Go 版本:$(go version)" # 清理所有重复的 package 声明 echo "" echo "检查重复的 package 声明..." for file in $(find . -name "*.go" -type f); do # 检查是否有重复的 package 行 if head -2 "$file" | grep -q "^package .*"; then first=$(head -1 "$file") second=$(head -2 "$file" | tail -1) if [[ "$first" =~ ^package ]] && [[ "$second" =~ ^package ]]; then echo "⚠️ 发现重复 package: $file" # 删除第二行 sed -i '2d' "$file" fi fi done echo "✓ 重复 package 声明已修复" # 下载依赖 echo "" echo "下载依赖..." go mod download go mod tidy echo "✓ 依赖已下载" # 格式化代码 echo "" echo "格式化代码..." go fmt ./... echo "✓ 代码已格式化" # 尝试编译 echo "" echo "编译项目..." if go build -v -o bin/gomog ./cmd/server 2>&1; then echo "✅ 编译成功!" echo "" echo "二进制文件:./bin/gomog" else echo "❌ 编译失败,请检查错误信息" exit 1 fi # 运行测试(如果存在) echo "" echo "运行测试..." if go test ./... -v 2>&1 | head -50; then echo "✓ 测试通过" else echo "⚠️ 部分测试可能失败或没有测试" fi echo "" echo "======================================" echo " 完成!" echo "======================================" echo "" echo "启动服务器:" echo " ./bin/gomog -config config.yaml" echo ""