{"id":205839,"date":"2025-06-12T08:29:22","date_gmt":"2025-06-12T00:29:22","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205839\/"},"modified":"2025-06-12T08:29:22","modified_gmt":"2025-06-12T00:29:22","slug":"golang-%e5%87%bd%e6%95%b0%e5%9c%a8-php-%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e4%b8%ad%e7%9a%84%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205839\/","title":{"rendered":"Golang \u51fd\u6570\u5728 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5e94\u7528"},"content":{"rendered":"<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300aGOLANG \u51fd\u6570\u5728 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5e94\u7528\u300b\uff0c\u8986\u76d6\u4e86GOLANG\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002,\u5c06 Go \u51fd\u6570\u96c6\u6210\u5230 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\u53ef\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a\u542f\u7528 CGO\u5728 Go \u4e2d\u7f16\u5199\u5bfc\u51fa\u51fd\u6570\u5e76\u7f16\u8bd1\u5728 PHP \u4e2d\u8c03\u7528 Go \u51fd\u6570\u901a\u8fc7\u52a0\u8f7d\u5171\u4eab\u5e93\u548c\u83b7\u53d6\u51fd\u6570\u6307\u9488\u6765\u8c03\u7528 Go \u51fd\u6570\u901a\u8fc7\u96c6\u6210 Go \u51fd\u6570\uff0cPHP \u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u5229\u7528 Go \u7684\u6027\u80fd\u4f18\u52bf,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730015009671def216824d.jpg\" class=\"aligncenter\">,<br \/>\n<strong>\u5728 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528 Go \u51fd\u6570<\/strong>,PHP \u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u811a\u672c\u8bed\u8a00\uff0c\u7528\u4e8e Web \u5f00\u53d1\u3002Go\uff08\u53c8\u79f0 GOLANG\uff09\u662f\u4e00\u79cd\u7f16\u8bd1\u8bed\u8a00\uff0c\u4ee5\u5176\u9ad8\u6027\u80fd\u548c\u5e76\u53d1\u6027\u800c\u95fb\u540d\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u5982\u4f55\u5c06 Go \u51fd\u6570\u96c6\u6210\u5230 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u4ee5\u5229\u7528\u8fd9\u4e24\u8005\u7684\u4f18\u70b9\u3002,<br \/>\n<strong>\u524d\u63d0\u6761\u4ef6\uff1a<\/strong>,<br \/>\n<strong>\u4f7f\u7528 CGO\uff1a<\/strong>,CGO \u662f\u4e00\u4e2a\u5141\u8bb8 PHP \u548c Go \u4ee3\u7801\u4ea4\u4e92\u7684\u5de5\u5177\u3002\u8981\u4f7f\u7528 CGO\uff0c\u9700\u8981\u5728 PHP \u9879\u76ee\u4e2d\u542f\u7528\u5b83\uff0c\u5e76\u7f16\u5199\u4e00\u4e2a Go \u51fd\u6570\u6765\u516c\u5f00\u7ed9 PHP\u3002,\u5728 PHP \u4e2d\u542f\u7528 CGO\uff1a,\u5728 Go \u4e2d\u7f16\u5199\u51fd\u6570\uff1a,<br \/>\n<strong>\u6784\u5efa Go \u51fd\u6570\uff1a<\/strong>,\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u7f16\u8bd1 Go \u51fd\u6570\uff1a,<br \/>\n<strong>\u5728 PHP \u4e2d\u8c03\u7528 Go \u51fd\u6570\uff1a<\/strong>,<br \/>\n<strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong>,\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a PHP \u811a\u672c\uff0c\u5b83\u9700\u8981\u751f\u6210\u4e00\u4e2a\u5305\u542b\u4e2a\u4eba\u8be6\u7ec6\u4fe1\u606f\u7684 PDF \u6587\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Go \u51fd\u6570\u6765\u5904\u7406 PDF \u751f\u6210\uff0c\u5145\u5206\u5229\u7528 Go \u7684\u6027\u80fd\u4f18\u52bf\u3002,Go \u51fd\u6570\uff1a,PHP \u811a\u672c\uff1a,\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 Go \u51fd\u6570\u96c6\u6210\u5230 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u548c\u6269\u5c55\u5e94\u7528\u7a0b\u5e8f\u7684\u529f\u80fd\u3002,\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGOLANG\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~,\u5728\u7cfb\u7edf\u4e0a\u5b89\u88c5 Go \u7f16\u8bd1\u5668,\u5728\u7cfb\u7edf\u4e0a\u5b89\u88c5 PHP,\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300aGOLANG \u51fd\u6570\u5728 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5e94\u7528\u300b\uff0c\u8986\u76d6\u4e86GOLANG\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>GOLANG \u51fd\u6570\u5728 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5e94\u7528<\/span><\/p>\n<h1>GOLANG \u51fd\u6570\u5728 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5e94\u7528<\/h1>\n<p><span>2024-10-27 15:43:44<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300aGOLANG \u51fd\u6570\u5728 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5e94\u7528\u300b\uff0c\u8986\u76d6\u4e86GOLANG\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p>\u5c06 Go \u51fd\u6570\u96c6\u6210\u5230 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\u53ef\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a\u542f\u7528 CGO\u5728 Go \u4e2d\u7f16\u5199\u5bfc\u51fa\u51fd\u6570\u5e76\u7f16\u8bd1\u5728 PHP \u4e2d\u8c03\u7528 Go \u51fd\u6570\u901a\u8fc7\u52a0\u8f7d\u5171\u4eab\u5e93\u548c\u83b7\u53d6\u51fd\u6570\u6307\u9488\u6765\u8c03\u7528 Go \u51fd\u6570\u901a\u8fc7\u96c6\u6210 Go \u51fd\u6570\uff0cPHP \u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u5229\u7528 Go \u7684\u6027\u80fd\u4f18\u52bf<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730015009671def216824d.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5728 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528 Go \u51fd\u6570<\/strong><\/p>\n<p>PHP \u662f\u4e00\u79cd\u5e7f\u6cdb\u4f7f\u7528\u7684\u811a\u672c\u8bed\u8a00\uff0c\u7528\u4e8e Web \u5f00\u53d1\u3002Go\uff08\u53c8\u79f0 GOLANG\uff09\u662f\u4e00\u79cd\u7f16\u8bd1\u8bed\u8a00\uff0c\u4ee5\u5176\u9ad8\u6027\u80fd\u548c\u5e76\u53d1\u6027\u800c\u95fb\u540d\u3002\u672c\u6587\u5c06\u63a2\u8ba8\u5982\u4f55\u5c06 Go \u51fd\u6570\u96c6\u6210\u5230 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u4ee5\u5229\u7528\u8fd9\u4e24\u8005\u7684\u4f18\u70b9\u3002<\/p>\n<p><strong>\u524d\u63d0\u6761\u4ef6\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u7cfb\u7edf\u4e0a\u5b89\u88c5 PHP<\/li>\n<li>\u5728\u7cfb\u7edf\u4e0a\u5b89\u88c5 Go \u7f16\u8bd1\u5668<\/li>\n<\/ul>\n<p><strong>\u4f7f\u7528 CGO\uff1a<\/strong><\/p>\n<p>CGO \u662f\u4e00\u4e2a\u5141\u8bb8 PHP \u548c Go \u4ee3\u7801\u4ea4\u4e92\u7684\u5de5\u5177\u3002\u8981\u4f7f\u7528 CGO\uff0c\u9700\u8981\u5728 PHP \u9879\u76ee\u4e2d\u542f\u7528\u5b83\uff0c\u5e76\u7f16\u5199\u4e00\u4e2a Go \u51fd\u6570\u6765\u516c\u5f00\u7ed9 PHP\u3002<\/p>\n<p>\u5728 PHP \u4e2d\u542f\u7528 CGO\uff1a<\/p>\n<pre>extension=php_cgo.so \/\/ \u5728 php.ini \u4e2d\u6dfb\u52a0\u6b64\u884c<\/pre>\n<p>\u5728 Go \u4e2d\u7f16\u5199\u51fd\u6570\uff1a<\/p>\n<pre>package main\n\n\/\/export SayHello\nfunc SayHello(name string) string {\n  return \"Hello, \" + name + \"!\"\n}<\/pre>\n<p><strong>\u6784\u5efa Go \u51fd\u6570\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u7f16\u8bd1 Go \u51fd\u6570\uff1a<\/p>\n<pre>go build -buildmode=c-shared -o hellogo.so main.go<\/pre>\n<p><strong>\u5728 PHP \u4e2d\u8c03\u7528 Go \u51fd\u6570\uff1a<\/strong><\/p>\n<pre>&lt;?php\n$name = \"John\";\n\n\/\/ \u52a0\u8f7d Go \u5171\u4eab\u5e93\n$cgo_handle = cgo_load('hellogo.so');\n\n\/\/ \u83b7\u53d6\u51fd\u6570\u6307\u9488\n$sayHello = $cgo_handle-&gt;SayHello;\n\n\/\/ \u8c03\u7528 Go \u51fd\u6570\n$greeting = $sayHello($name);\n\necho $greeting;<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a PHP \u811a\u672c\uff0c\u5b83\u9700\u8981\u751f\u6210\u4e00\u4e2a\u5305\u542b\u4e2a\u4eba\u8be6\u7ec6\u4fe1\u606f\u7684 PDF \u6587\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 Go \u51fd\u6570\u6765\u5904\u7406 PDF \u751f\u6210\uff0c\u5145\u5206\u5229\u7528 Go \u7684\u6027\u80fd\u4f18\u52bf\u3002<\/p>\n<p>Go \u51fd\u6570\uff1a<\/p>\n<pre>package pdf\n\n\/\/export GeneratePDF\nfunc GeneratePDF(data map[string]string, template string) error {\n  \/\/ PDF \u751f\u6210\u4ee3\u7801...\n}<\/pre>\n<p>PHP \u811a\u672c\uff1a<\/p>\n<pre>&lt;?php\nuse PDF\\GeneratePDF;\n\n$data = [\n  'name' =&gt; 'John Doe',\n  'email' =&gt; 'johndoe@example.com'\n];\n$template = 'template.pdf';\n\ntry {\n  GeneratePDF($data, $template);\n  echo \"PDF generated successfully!\";\n} catch (Exception $e) {\n  echo \"Error generating PDF: \" . $e-&gt;getMessage();\n}<\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06 Go \u51fd\u6570\u96c6\u6210\u5230 PHP \u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u548c\u6269\u5c55\u5e94\u7528\u7a0b\u5e8f\u7684\u529f\u80fd\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGOLANG\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u4e09\u661f\u7535\u8111270e5j\u600e\u4e48\u6062\u590d\u51fa\u5382\u8bbe\u7f6e\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u9ad8\u9636\u51fd\u6570\u5728 Java \u4e2d\u5b9e\u73b0\u94fe\u5f0f\u7f16\u7a0b\u7684\u4f18\u52bf\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300aGOLANG&#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-205839","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205839","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=205839"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205839\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}