{"id":274778,"date":"2021-01-12T08:31:47","date_gmt":"2021-01-12T00:31:47","guid":{"rendered":"http:\/\/4563.org\/?p=274778"},"modified":"2021-01-12T08:31:47","modified_gmt":"2021-01-12T00:31:47","slug":"go-%e7%9a%84-cgo-%e6%80%8e%e4%b9%88%e7%94%a8%e5%95%8a%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=274778","title":{"rendered":"go \u7684 cgo \u600e\u4e48\u7528\u554a\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  go \u7684 cgo \u600e\u4e48\u7528\u554a\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lysS <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u76f4\u63a5\u62a5\u9519\uff1a<\/p>\n<pre><code>package main  \/\/ #include &lt;stdio.h&gt; \/\/ #include &lt;stdlib.h&gt; \/* void print(char *str) {     printf(\"%sn\", str); } *\/ import \"C\"  import \"unsafe\"  func main() {  s := \"Hello Cgo\"  cs := C.CString(s)  C.print(cs)  C.free(unsafe.Pointer(cs)) }  <\/code><\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" referrerpolicy=\"no-referrer\" rel=\"noreferrer\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/01\/20210113_5ffe5857c3b41.png\" alt=\"go \u7684 cgo \u600e\u4e48\u7528\u554a\uff1f\" \/><\/p>\n<p><strong>\u8bf4\u4e00\u4e0b\u6211\u7684\u9700\u6c42\uff1a<\/strong>golang \u7f16\u8bd1\u7684 dll \u52a8\u6001\u5e93\u8f93\u51fa\u5b57\u7b26\u4e32<br \/> \u5982\u679c\u662f\u8fd9\u6bb5\u4ee3\u7801\uff1a<\/p>\n<pre><code>import \"C\"  func main() {  Hello_world() }  \/\/export Hello_world func Hello_world() string {  return \"hello dart!!\" } <\/code><\/pre>\n<p>\u7f16\u8bd1\u7684 dll\uff0c\u4f7f\u7528 py \u8c03\u7528\u7684\u7ed3\u679c\u662f\u4e00\u4e32\u6570\u5b57<code>830403984<\/code><\/p>\n<pre><code>from ctypes import CDLL, c_char_p  dll = CDLL(\".\/hello_world.dll\") r = dll.Hello_world() print(r) <\/code><\/pre>\n<p>\u67e5\u4e86\u4e00\u4e0b\uff0c\u9700\u8981\u628a go \u7684 string \u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684 C \u7684\u7c7b\u578b\uff0c\u5c31\u6709\u6700\u5f00\u59cb\u90a3\u4e2a\u95ee\u9898\uff1b \u7ed9\u6211\u6574\u8499\u4e86\u3002\u3002\u3002\u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>go \u7684 cgo \u600e\u4e48\u7528\u554a\uff1f \u8cc7\u6df1&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/274778"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=274778"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/274778\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=274778"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=274778"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=274778"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}