{"id":207493,"date":"2025-07-08T12:14:24","date_gmt":"2025-07-08T04:14:24","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207493\/"},"modified":"2025-07-08T12:14:24","modified_gmt":"2025-07-08T04:14:24","slug":"%e5%9c%a8%e5%91%bd%e4%bb%a4%e8%a1%8c%e4%b8%8a%e8%bf%90%e8%a1%8c%e5%8f%af%e6%89%a7%e8%a1%8c%e6%96%87%e4%bb%b6%e6%b2%a1%e9%97%ae%e9%a2%98%ef%bc%8c%e4%bd%86%e9%80%9a%e8%bf%87%e5%8f%a6%e4%b8%80%e4%b8%aa","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207493\/","title":{"rendered":"\u5728\u547d\u4ee4\u884c\u4e0a\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6\u6ca1\u95ee\u9898\uff0c\u4f46\u901a\u8fc7\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u8fd0\u884c\u4f1a\u5bfc\u81f4\u65e0\u54cd\u5e94"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5728\u547d\u4ee4\u884c\u4e0a\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6\u6ca1\u95ee\u9898\uff0c\u4f46\u901a\u8fc7\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u8fd0\u884c\u4f1a\u5bfc\u81f4\u65e0\u54cd\u5e94<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 08:00:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5728\u547d\u4ee4\u884c\u4e0a\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6\u6ca1\u95ee\u9898\uff0c\u4f46\u901a\u8fc7\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u8fd0\u884c\u4f1a\u5bfc\u81f4\u65e0\u54cd\u5e94\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5728 Windows \u547d\u4ee4\u63d0\u793a\u7b26\u4e0b\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6\u4ee5\u53ca\u53c2\u6570\u5c31\u53ef\u4ee5\u4e86\uff1a<\/p>\n<pre>cgx_STATIC.exe -b C:\\Users\\m3\\AppData\\Local\\Temp\\shot-277325955.fbd<\/pre>\n<p>\u4f46\u662f\uff0c\u5f53\u901a\u8fc7 Golang \u8fd0\u884c\u76f8\u540c\u7684\u53ef\u6267\u884c\u6587\u4ef6\u65f6\uff0c\u5728\u53ef\u6267\u884c\u6587\u4ef6\u521b\u5efa\u4e00\u4e9b\u8f93\u51fa\u6587\u4ef6\u540e\uff0c\u53ef\u6267\u884c\u6587\u4ef6\u5c06\u53d8\u5f97\u65e0\u54cd\u5e94\u3002<\/p>\n<pre><code>\n\/\/ Run an executable and print its log into a file.\nfunc RunWithLogFile(pthExe string, arg []string, fLog *os.File) error {\n    cmd := exec.Command(pthExe, arg...)\n\n    stdout, err := cmd.StdoutPipe()\n    if err != nil {\n        return err\n    }\n    stderr, err := cmd.StderrPipe()\n    if err != nil {\n        return err\n    }\n\n    err = cmd.Start()\n    if err != nil {\n        return err\n    }\n\n    \/\/ Stream logs:\n    \/\/ https:\/\/stackoverflow.com\/a\/48849811\/3405291\n    scannerOut := bufio.NewScanner(stdout)\n    scannerErr := bufio.NewScanner(stderr)\n    scannerOut.Split(bufio.ScanRunes)\n    scannerErr.Split(bufio.ScanRunes)\n    for scannerOut.Scan() {\n        _, err = fLog.WriteString(scannerOut.Text())\n        if err != nil {\n            return err\n        }\n    }\n    for scannerErr.Scan() {\n        _, err = fLog.WriteString(scannerErr.Text())\n        if err != nil {\n            return err\n        }\n    }\n    if scannerOut.Err() != nil {\n        return err\n    }\n    if scannerErr.Err() != nil {\n        return err\n    }\n\n    err = cmd.Wait()\n    return err\n}\n<\/code><\/pre>\n<p>\u6211\u60f3\u77e5\u9053\u4e0a\u9762\u7684 Go \u4ee3\u7801\u662f\u5426\u5b58\u5728\u67d0\u79cd\u9519\u8bef\u6216\u4e0d\u9002\u5408\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6\uff1f<\/p>\n<h1>\u5355\u72ec\u7684 goroutine<\/h1>\n<p>\u6309\u7167@BurakSerdar\u7684\u5efa\u8bae\uff0c\u6211\u5728\u5355\u72ec\u7684goroutines\u4e2d\u4ecestdout\u548cstderr\u8bfb\u53d6\u5185\u5bb9\uff0c\u4f46\u95ee\u9898\u6ca1\u6709\u5f97\u5230\u89e3\u51b3\uff1a<\/p>\n<pre><code>\n\/\/ Run an executable and print its log into a file.\nfunc RunWithLogFile(pthExe string, arg []string, fLog *os.File) error {\n    cmd := exec.Command(pthExe, arg...)\n\n    stdout, err := cmd.StdoutPipe()\n    if err != nil {\n        return err\n    }\n    stderr, err := cmd.StderrPipe()\n    if err != nil {\n        return err\n    }\n\n    var wg sync.WaitGroup\n    wg.Add(2)\n\n    go streamToLogFile(stdout, fLog, &amp;wg)\n    go streamToLogFile(stderr, fLog, &amp;wg)\n\n    err = cmd.Start()\n    if err != nil {\n        return err\n    }\n\n    wg.Wait()\n\n    err = cmd.Wait()\n    return err\n}\n\nfunc streamToLogFile(output io.ReadCloser, fLog *os.File, wg *sync.WaitGroup) {\n    defer wg.Done()\n    scanner := bufio.NewScanner(output)\n    scanner.Split(bufio.ScanRunes)\n    for scanner.Scan() {\n        _, err := fLog.WriteString(scanner.Text())\n        if err != nil {\n            log.Printf(\"error: write to log file: %s\", err.Error())\n        }\n    }\n    err := scanner.Err()\n    if err != nil {\n        log.Printf(\"error: write to log file: %s\", err.Error())\n    }\n}\n<\/code><\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u901a\u8fc7\u4f7f\u7528<strong>\u7ba1\u7406\u5458<\/strong><strong>\u6743\u9650<\/strong>\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6\u89e3\u51b3\u4e86\u65e0\u54cd\u5e94\u95ee\u9898\u3002\u6211\u6b63\u5728\u8fd9\u6837\u505a\uff1a<\/p>\n<pre>A C# code runs a Go code and the Go code runs a C Code, i.e. external executable.<\/pre>\n<p>C \u53ef\u6267\u884c\u6587\u4ef6\u8c03\u7528\u4e00\u4e9b OpenGL GLUT \u8c03\u7528\u3002\u4e5f\u8bb8\u4ed6\u4eec\u9700\u8981<strong>\u7ba1\u7406\u5458\u6743\u9650<\/strong>\u3002<\/p>\n<p>\u901a\u8fc7 C# \u542f\u52a8 Go \u4ee3\u7801\u89e3\u51b3\u4e86\u8fd9\u4e2a\u95ee\u9898\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre><code>        public static void RunLogic(string exePath, string args, PostProcess pp)\n        {\n            cmd = new Process();\n\n            try\n            {\n                cmd.StartInfo.FileName = exePath;\n                cmd.StartInfo.Arguments = args;\n                cmd.StartInfo.UseShellExecute = true;\n                cmd.StartInfo.CreateNoWindow = false;\n                cmd.StartInfo.RedirectStandardOutput = false;\n                cmd.StartInfo.RedirectStandardError = false;\n                cmd.StartInfo.RedirectStandardInput = false;\n                \/\/ Vista or higher check.\n                \/\/ https:\/\/stackoverflow.com\/a\/2532775\/3405291\n                if (System.Environment.OSVersion.Version.Major &gt;= 6)\n                {\n                    \/\/ Run with admin privileges to avoid a non-responsive executable.\n                    cmd.StartInfo.Verb = \"runas\";\n                }\n                cmd.EnableRaisingEvents = true;\n                cmd.Exited += new EventHandler(cmd_Exited);\n                cmd.Exited += new EventHandler(pp);\n\n                cmd.Start();\n            }\n\n            catch (Exception ex)\n            {\n                RhinoApp.WriteLine(\"Error on process start: {0}\", ex.Message);\n            }\n        }\n<\/code><\/pre>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5728\u547d\u4ee4\u884c\u4e0a\u8fd0\u884c\u53ef\u6267\u884c\u6587\u4ef6\u6ca1\u95ee\u9898\uff0c\u4f46\u901a\u8fc7\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u8fd0\u884c\u4f1a\u5bfc\u81f4\u65e0\u54cd\u5e94\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207493","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207493"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207493\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}