{"id":208080,"date":"2025-07-08T10:53:49","date_gmt":"2025-07-08T02:53:49","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208080\/"},"modified":"2025-07-08T10:53:49","modified_gmt":"2025-07-08T02:53:49","slug":"%e4%bd%bf%e7%94%a8-cgo-%e6%9e%84%e5%bb%ba%e6%97%b6%e5%a6%82%e4%bd%95%e8%b0%83%e8%af%95-%e8%bd%ac%e5%82%a8-go-%e5%8f%98%e9%87%8f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208080\/","title":{"rendered":"\u4f7f\u7528 cgo \u6784\u5efa\u65f6\u5982\u4f55\u8c03\u8bd5\/\u8f6c\u50a8 Go \u53d8\u91cf\uff1f"},"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>\u4f7f\u7528 cgo \u6784\u5efa\u65f6\u5982\u4f55\u8c03\u8bd5\/\u8f6c\u50a8 Go \u53d8\u91cf\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-28 20:12:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5bf9\u4e8e\u4e00\u4e2aGolang\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0c\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300a\u4f7f\u7528 cgo \u6784\u5efa\u65f6\u5982\u4f55\u8c03\u8bd5\/\u8f6c\u50a8 Go \u53d8\u91cf\uff1f\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u7528 cgo \u5728 go \u4e2d\u7f16\u5199\u4e00\u4e2a mysql udf\uff0c\u5176\u4e2d\u6211\u6709\u4e00\u4e2a\u57fa\u672c\u7684\u529f\u80fd\uff0c\u4f46\u662f\u6709\u4e00\u4e9b\u6211\u65e0\u6cd5\u5f04\u6e05\u695a\u7684\u70b9\u70b9\u6ef4\u6ef4\uff0c\u56e0\u4e3a\u6211\u4e0d\u77e5\u9053\u4e00\u4e9b c \u53d8\u91cf\u662f\u4ec0\u4e48\u662f\u7528 go \u8bed\u8a00\u6765\u8bf4\u7684\u3002 <\/p>\n<p>\u8fd9\u662f\u6211\u7528 c \u8bed\u8a00\u7f16\u5199\u7684\u4e00\u4e2a\u793a\u4f8b\uff0c\u5b83\u5f3a\u5236 mysql \u53c2\u6570\u4e4b\u4e00\u7684\u7c7b\u578b\u4e3a int<\/p>\n<pre>my_bool unhex_sha3_init(udf_init *initid, udf_args *args, char *message) {\n    if (args-&gt;arg_count != 2) {\n        strcpy(message, \"`unhex_sha3`() requires 2 parameters: the message part, and the bits\");\n        return 1;\n    }\n\n    args-&gt;arg_type[1] = int_result;\n\n    initid-&gt;maybe_null = 1; \/\/can return null\n\n    return 0;\n}<\/pre>\n<p>\u8fd9\u5de5\u4f5c\u5f97\u5f88\u597d\uff0c\u4f46\u662f\u6211\u5c1d\u8bd5\u7528 go \u4e2d\u7684\u53e6\u4e00\u4e2a\u51fd\u6570\u505a\u76f8\u540c\/\u7c7b\u4f3c\u7684\u4e8b\u60c5\uff0c\u5982\u4e0b\u6240\u793a<\/p>\n<pre>\/\/export get_url_param_init\nfunc get_url_param_init(initid *c.udf_init, args *c.udf_args, message *c.char) c.my_bool {\n    if args.arg_count != 2 {\n        message = c.cstring(\"`get_url_param` require 2 parameters: the url string and the param name\")\n        return 1\n    }\n\n    (*args.arg_type)[0] = c.string_result\n    (*args.arg_type)[1] = c.string_result\n\n    initid.maybe_null = 1\n\n    return 0\n}<\/pre>\n<p>\u51fa\u73b0\u6b64\u6784\u5efa\u9519\u8bef<\/p>\n<p> .\/main.go:24: \u65e0\u6548\u64cd\u4f5c: (*args.arg_type)[0] (\u7c7b\u578b uint32 \u786e\u5b9e \u4e0d\u652f\u6301\u7d22\u5f15\uff09 <\/p>\n<p>\u6211\u4e0d\u592a\u786e\u5b9a\u8fd9\u610f\u5473\u7740\u4ec0\u4e48\u3002\u8fd9\u4e0d\u5e94\u8be5\u662f\u67d0\u79cd\u5207\u7247\uff0c\u800c\u4e0d\u662f <code>uint32<\/code> \u5417\uff1f<\/p>\n<p>\u8fd9\u5c31\u662f\u4ee5\u67d0\u79cd\u65b9\u5f0f\u5c06 <code>args<\/code> \u7ed3\u6784\u8f6c\u50a8\u5230\u67d0\u5904\uff08\u751a\u81f3\u53ef\u80fd\u5728 go \u8bed\u6cd5\u4e2d\u4f5c\u4e3a\u8d85\u7ea7\u4f18\u70b9\uff09\u7684\u65b9\u6cd5\uff0c\u8fd9\u6837\u6211\u5c31\u53ef\u4ee5\u77e5\u9053\u6211\u6b63\u5728\u4f7f\u7528\u4ec0\u4e48\u3002<\/p>\n<p>\u597d\u5427\uff0c\u6211\u4f7f\u7528 spew \u5c06\u53d8\u91cf\u5185\u5bb9\u8f6c\u50a8\u5230 init \u51fd\u6570\u5185\u7684 tmp \u6587\u4ef6\u4e2d\uff08\u6ce8\u91ca\u6389\u5bfc\u81f4\u65e0\u6cd5\u7f16\u8bd1\u7684\u884c\uff09\uff0c\u6211\u5f97\u5230\u4e86\u8fd9\u4e2a<\/p>\n<pre>(string) (len=3) \"%#v\"\n(*main._Ctype_struct_st_udf_args)(0x7ff318006af8)({\n arg_count: (main._Ctype_uint) 2,\n _: ([4]uint8) (len=4 cap=4) {\n  00000000  00 00 00 00                                       |....|\n },\n arg_type: (*uint32)(0x7ff318006d18)(0),\n args: (**main._Ctype_char)(0x7ff318006d20-&gt;0x7ff3180251b0)(0),\n lengths: (*main._Ctype_ulong)(0x7ff318006d30)(0),\n maybe_null: (*main._Ctype_char)(0x7ff318006d40)(0),\n attributes: (**main._Ctype_char)(0x7ff318006d58-&gt;0x7ff318006b88)(39),\n attribute_lengths: (*main._Ctype_ulong)(0x7ff318006d68)(2),\n extension: (unsafe.Pointer) &lt;nil&gt;\n})<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u597d\u5427\uff0c@jimb \u7ed9\u4e86\u6211\u5f88\u5927\u7684\u5e2e\u52a9\uff0c\u5c3d\u7ba1\u6211\u663e\u7136\u4e0d\u592a\u64c5\u957f go\uff08\u5c24\u5176\u662f cgo\uff09\uff0c\u4f46\u6211\u5df2\u7ecf\u6709\u4e86 udf \u7684\u5de5\u4f5c\u7248\u672c\uff0c\u8fd9\u662f\u4e00\u4e2a\u7b80\u5355\u800c\u76f4\u63a5\u7684\u7248\u672c\uff08\u5e76\u4e14fast\uff09\u51fd\u6570\uff0c\u4ece url \u5b57\u7b26\u4e32\u4e2d\u63d0\u53d6\u5355\u4e2a\u53c2\u6570\u5e76\u6b63\u786e\u89e3\u7801\u5b83\uff0c\u4ee5\u53ca\u4e0d\u6b63\u786e\u7684\u89e3\u7801\uff08\u4f8b\u5982 %20 \u4f5c\u4e3a\u7a7a\u683c\u8fd4\u56de\uff0c\u57fa\u672c\u4e0a\u662f\u60a8\u671f\u671b\u7684\u5de5\u4f5c\u65b9\u5f0f\uff09\u3002<\/p>\n<p>\u8fd9\u5bf9\u4e8e\u7eaf c udf \u6765\u8bf4\u4f3c\u4e4e\u975e\u5e38\u68d8\u624b\uff0c\u56e0\u4e3a\u6211\u5e76\u4e0d\u771f\u6b63\u4e86\u89e3 c\uff08\u4ee5\u53ca\u6211\u4e86\u89e3\u5176\u4ed6\u8bed\u8a00\uff09\uff0c\u5e76\u4e14 url \u89e3\u6790\u548c url \u53c2\u6570\u89e3\u7801\u4ee5\u53ca\u672c\u673a mysql \u51fd\u6570\u53ef\u80fd\u4f1a\u51fa\u73b0\u5f88\u591a\u9519\u8bef<em>\u6162<\/em>\uff08\u800c\u4e14\u4e5f\u6ca1\u6709\u4e00\u79cd\u771f\u6b63\u597d\u7684\u3001\u5e72\u51c0\u7684\u65b9\u6cd5\u6765\u8fdb\u884c\u89e3\u7801\uff09\uff0c\u56e0\u6b64 go \u4f3c\u4e4e\u662f\u89e3\u51b3\u6b64\u7c7b\u95ee\u9898\u7684\u6700\u4f73\u5019\u9009\u8005\uff0c\u56e0\u4e3a\u5b83\u5177\u6709\u5f3a\u5927\u7684\u6027\u80fd\uff0c\u6613\u4e8e\u7f16\u5199\uff0c\u4ee5\u53ca\u5404\u79cd\u6613\u4e8e\u4f7f\u7528\u7684\u5185\u7f6e\u63d2\u4ef6\u548c\u7b2c\u4e09\u65b9\u5e93\u3002<\/p>\n<p>\u5b8c\u6574\u7684 udf \u53ca\u5176\u5b89\u88c5\/\u4f7f\u7528\u8bf4\u660e\u4f4d\u4e8e\u6b64\u5904 <\/p>\n<p>\u7b2c\u4e00\u4e2a\u95ee\u9898\u662f\u8c03\u8bd5\u8f93\u51fa\u3002\u6211\u901a\u8fc7 <code>fprintf<\/code>ing \u5230 tmp \u6587\u4ef6\u800c\u4e0d\u662f\u6807\u51c6\u8f93\u51fa\u6765\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u8fd9\u6837\u6211\u5c31\u53ef\u4ee5\u68c0\u67e5\u6587\u4ef6\u4ee5\u67e5\u770b\u53d8\u91cf\u8f6c\u50a8\u3002<\/p>\n<pre>t, err := ioutil.tempfile(os.tempdir(), \"get-url-param\")\n\nfmt.fprintf(t, \"%#v\\n\", args.arg_type)<\/pre>\n<p>\u7136\u540e\u5728\u6211\u5f97\u5230\u8f93\u51fa\u540e\uff08\u6211\u671f\u671b args.arg_type \u662f\u4e00\u4e2a\u50cf c \u4e2d\u90a3\u6837\u7684\u6570\u7ec4\uff0c\u4f46\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u6570\u5b57\uff09\uff0c\u6211\u9700\u8981\u8f6c\u6362\u8be5\u6570\u5b57\u5f15\u7528\u7684\u6570\u636e\uff08\u6307\u5411c \u6570\u7ec4\uff09\u5230 go \u6570\u7ec4\uff0c\u8fd9\u6837\u6211\u5c31\u53ef\u4ee5\u8bbe\u7f6e\u5b83\u7684\u503c\u3002<\/p>\n<pre>argsTypes := *(*[2]uint32)(unsafe.Pointer(args.arg_type))\n\nargsTypes[0] = C.STRING_RESULT\nargsTypes[1] = C.STRING_RESULT<\/pre>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300a\u4f7f\u7528 cgo \u6784\u5efa\u65f6\u5982\u4f55\u8c03\u8bd5\/\u8f6c\u50a8 Go \u53d8\u91cf\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/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-208080","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208080","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=208080"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208080\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}