{"id":208324,"date":"2025-07-08T16:10:35","date_gmt":"2025-07-08T08:10:35","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208324\/"},"modified":"2025-07-08T16:10:35","modified_gmt":"2025-07-08T08:10:35","slug":"%e7%9b%b4%e6%8e%a5c%e6%8c%87%e9%92%88%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208324\/","title":{"rendered":"\u76f4\u63a5C\u6307\u9488\u8f6c\u6362"},"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>\u76f4\u63a5C\u6307\u9488\u8f6c\u6362<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-05-01 19:33:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300a\u76f4\u63a5C\u6307\u9488\u8f6c\u6362\u300b\uff0c\u804a\u804a\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6709\u8fd9\u4e2a c \u4ee3\u7801\uff1a<\/p>\n<pre>uint8_t *data[buf_size];\ndata = ...;\n\n\/\/ extern void goreaddata(uint8_t *data, int buffersize);\ngoreaddata(data, buf_size)<\/pre>\n<p>\u5728 go \u4ee3\u7801\u4e2d\uff0c\u6211\u5c1d\u8bd5\u4f7f\u7528 <code>data<\/code> \u6307\u9488\u4f5c\u4e3a go \u6570\u7ec4\u6216\u5207\u7247\uff0c\u6211\u60f3\u4ece *c.uint8_t \u68c0\u7d22 []uint8\u3002\u6211\u77e5\u9053 <code>data<\/code> \u7684\u5927\u5c0f<\/p>\n<pre>\/\/export goReadData\nfunc goReadData(data *C.uint8_t, bufferSize C.int) {\n\n    fmt.Printf(\"Data type %v\\n\", reflect.TypeOf(data))\n    \/\/ print 1: Data type *main._Ctype_uchar\n\n\n    \/\/ Solution 1: GoBytes\n    \/\/ works but really slow (memory copy I think)\n    goBytes := C.GoBytes(unsafe.Pointer(data), bufferSize)\n    fmt.Printf(\"goBytes type %v\\n\", reflect.TypeOf(goBytes))\n    \/\/ print 2: goBytes type []uint8\n\n\n    \/\/ Solution 2: direct pointer\n    \/\/ Really fast, but wrong type at the end\n    \/\/ unsafe.Pointer to the C array\n    unsafePtr := unsafe.Pointer(data)\n\n    \/\/ convert unsafePtr to a pointer of the type *[1 &lt;&lt; 30]C.uint8_t\n    arrayPtr := (*[1 &lt;&lt; 30]C.uint8_t)(unsafePtr)\n\n    \/\/ slice the array into a Go slice, with the same backing array\n    \/\/ as data, making sure to specify the capacity as well as\n    \/\/ the length.\n    length := int(bufferSize)\n    slice := arrayPtr[0:length:length]\n\n    fmt.Printf(\"Direct slice type %v\\n\", reflect.TypeOf(slice))\n    \/\/Print 3: Direct type []main._Ctype_uchar\n}<\/pre>\n<p>\u5982\u4f55\u4f7f\u7528\u7b2c\u4e8c\u4e2a\u89e3\u51b3\u65b9\u6848\u6062\u590d <code>[]uint8<\/code> \u800c\u4e0d\u662f []main._ctype_uchar \uff1f\u6216\u8005\u60a8\u662f\u5426\u6709\u53e6\u4e00\u79cd\u89e3\u51b3\u65b9\u6848\u53ef\u4ee5\u5728\u6ca1\u6709\u5b57\u8282\u590d\u5236\u7684\u60c5\u51b5\u4e0b\u505a\u5230\u8fd9\u4e00\u70b9\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5bf9\u4e0d\u8d77\uff0c\u5927\u5bb6\uff0c\u6211\u53d1\u73b0\u4e86\u81ea\u5df1\u7684\u9519\u8bef\uff1a<\/p>\n<pre>\/\/ convert unsafeptr to a pointer of the type *[1 &lt;&lt; 30]c.uint8_t\narrayptr := (*[1 &lt;&lt; 30]c.uint8_t)(unsafeptr)<\/pre>\n<p>==&gt;\u81f3<\/p>\n<pre>\/\/ convert unsafePtr to a pointer of the type *[1 &lt;&lt; 30]uint8\narrayPtr := (*[1 &lt;&lt; 30]uint8)(unsafePtr)<\/pre>\n<p>\u95ee\u9898\u89e3\u51b3\u4e86\uff01<\/p>\n<p>\u8c22\u8c22;)<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u76f4\u63a5C\u6307\u9488\u8f6c\u6362\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\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-208324","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208324","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=208324"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208324\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}