From eb52a18c3af7dbb90aaba4d5ffd7319ac7f8af72 Mon Sep 17 00:00:00 2001 From: joylink_zhangsai <1021828630@qq.com> Date: Mon, 28 Aug 2023 15:30:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9proto=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- protobuf/main/main.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/protobuf/main/main.go b/protobuf/main/main.go index 510fb23..de10565 100644 --- a/protobuf/main/main.go +++ b/protobuf/main/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "io/fs" "log" "os" "os/exec" @@ -25,23 +26,25 @@ func main() { } } -// 获取指定文件夹下的所有proto文件的绝对路径列表 +// 获取指定文件夹下的所有proto文件的绝对路径 func getProtoFiles() []string { var protoFiles []string - files, err := os.ReadDir(protoFolder) + err := filepath.WalkDir(protoFolder, func(path string, d fs.DirEntry, err error) error { + if !d.IsDir() { + protoFiles = append(protoFiles, path) + } + return err + }) if err != nil { - log.Fatal("获取proto文件列表失败") - } - for _, file := range files { - protoFiles = append(protoFiles, file.Name()) + log.Fatal("获取proto文件列表失败:", err) } return protoFiles } // 编译proto文件为Go文件 func compileProto(protoFiles []string) error { - for _, protoFile := range protoFiles { - cmd := exec.Command(protocPath, "--proto_path="+protoFolder, "--go_out=./", protoFile) + for _, fileName := range protoFiles { + cmd := exec.Command(protocPath, "-I="+protoFolder, "--go_out=./", fileName) fmt.Println(cmd.String()) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr