{"id":378711,"date":"2024-06-12T15:02:24","date_gmt":"2024-06-12T15:02:24","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=378711"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=378711","title":{"rendered":"<span>\u0412\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 Go \u0438\u0437 Python \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e ctypes<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u0443\u044e \u043d\u0430 Go \u0438\u0437 Python. \u0417\u0430\u0447\u0435\u043c? \u041f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u043d\u0430 Python \u0438\u043d\u043e\u0433\u0434\u0430 \u0438\u043c\u0435\u0435\u0442 \u0441\u043c\u044b\u0441\u043b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0430 \u0441\u0442\u0430\u0442\u0438\u0447\u043d\u043e\u043c, \u0432\u044b\u0441\u043e\u043a\u043e\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u043c \u044f\u0437\u044b\u043a\u0435. Go \u043c\u043e\u0436\u0435\u0442 \u0441\u0442\u0430\u0442\u044c \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u043c \u0432\u044b\u0431\u043e\u0440\u043e\u043c \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d \u0431\u044b\u0441\u0442\u0440\u044b\u0439, \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438 \u043a\u0440\u043e\u0441\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u044b\u0439. \u041d\u0435\u0434\u0430\u0432\u043d\u043e \u0432 \u043c\u043e\u0435\u043c <a href=\"https:\/\/t.me\/+jTQc-QM7D5YxNjgy\" rel=\"noopener noreferrer nofollow\">Python \u043a\u0430\u043d\u0430\u043b\u0435<\/a>, \u043c\u044b \u043e\u0431\u0441\u0443\u0436\u0434\u0430\u043b\u0438, \u043a\u0430\u043a \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0440\u043e\u0434\u0438\u043b\u0430\u0441\u044c \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f.<\/p>\n<p>\u0414\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u0441\u0435\u0445 \u0448\u0430\u0433\u043e\u0432 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f: Python, \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 Go \u0438 GCC (MinGW \u0434\u043b\u044f Windows). \u041f\u0440\u0438\u043c\u0435\u0440\u044b \u043a\u043e\u0434\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0432 \u0440\u0435\u043f\u043e \u043d\u0430 <a href=\"https:\/\/www.github.com\/fluhus\/snopher\" rel=\"noopener noreferrer nofollow\">Github<\/a>.<\/p>\n<p>\u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0432\u044b\u0437\u043e\u0432\u0430 Go \u0438\u0437 Python \u2014 <a href=\"http:\/\/swig.org\/\" rel=\"noopener noreferrer nofollow\">SWIG<\/a>, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440. \u0417\u0434\u0435\u0441\u044c \u0436\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c <code>ctypes<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u0438 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442.<\/p>\n<p>\u041f\u043e\u0435\u0445\u0430\u043b\u0438!<\/p>\n<h2>\u041f\u043b\u0430\u043d<\/h2>\n<ul>\n<li>\n<p><a href=\"#1\" rel=\"noopener noreferrer nofollow\">Hello world<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#2\" rel=\"noopener noreferrer nofollow\">\u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u0432\u043e\u0434 \u0438 \u0432\u044b\u0432\u043e\u0434<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#3\" rel=\"noopener noreferrer nofollow\">\u041c\u0430\u0441\u0441\u0438\u0432\u044b \u0438 \u0441\u0440\u0435\u0437\u044b<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#4\" rel=\"noopener noreferrer nofollow\">\u0421\u0442\u0440\u043e\u043a\u0438<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#5\" rel=\"noopener noreferrer nofollow\">\u041c\u0430\u0441\u0441\u0438\u0432 \u0441\u0442\u0440\u043e\u043a<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#6\" rel=\"noopener noreferrer nofollow\">Numpy \u0438 Pandas<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#7\" rel=\"noopener noreferrer nofollow\">\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#8\" rel=\"noopener noreferrer nofollow\">\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 <code>__del__<\/code><\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#9\" rel=\"noopener noreferrer nofollow\">\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#10\" rel=\"noopener noreferrer nofollow\">\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043e \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438<\/a><\/p>\n<ul>\n<li>\n<p><a href=\"#11\" rel=\"noopener noreferrer nofollow\">\u0421\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#12\" rel=\"noopener noreferrer nofollow\">\u041f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#13\" rel=\"noopener noreferrer nofollow\">\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <code>array<\/code> \u0434\u043b\u044f \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0438 \u043f\u0430\u043c\u044f\u0442\u0438<\/a><\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><a href=\"#14\" rel=\"noopener noreferrer nofollow\">\u0411\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0438<\/a><\/p>\n<ul>\n<li>\n<p><a href=\"#15\" rel=\"noopener noreferrer nofollow\">\u0412\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u03c0<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#16\" rel=\"noopener noreferrer nofollow\">\u041f\u0435\u0440\u0435\u043c\u0435\u0448\u0438\u0432\u0430\u043d\u0438\u0435 10M \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#17\" rel=\"noopener noreferrer nofollow\">\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>array<\/code> \u0438 \u043c\u0435\u0442\u043e\u0434, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u043e\u0432\u0430\u043d\u043d\u044b\u0439 <code>ctypes<\/code><\/a><\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><a class=\"anchor\" name=\"1\" id=\"1\"><\/a><\/p>\n<h2>Hello world<\/h2>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 hello-world, \u043a\u0443\u0434\u0430 \u0436\u0435 \u0431\u0435\u0437 \u043d\u0435\u0433\u043e<\/p>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/hello\/hello.go\" rel=\"noopener noreferrer nofollow\"><strong>hello.go<\/strong><\/a><\/p>\n<pre><code class=\"go\">package main  import \"C\" import \"fmt\"  \/\/export hello func hello() {     fmt.Println(\"Hello world!\") }  func main() {} <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 <code>hello.go<\/code> \u0444\u0430\u0439\u043b <code>hello.dll<\/code>  \u2014 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c \u043f\u0435\u0440\u0432\u044b\u0439 \u0441 \u0444\u043b\u0430\u0433\u043e\u043c <code>-buildmode=c-shared<\/code><\/p>\n<pre><code class=\"bash\"># Windows: go build -o hello.dll -buildmode=c-shared hello.go # Linux: go build -o hello.so -buildmode=c-shared hello.go <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <code>hello.dll<\/code> \u0432 Python \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0444\u0430\u0439\u043b, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <code>ctypes.CDLL()<\/code>:<\/p>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/hello\/hello.py\" rel=\"noopener noreferrer nofollow\"><strong>hello.py<\/strong><\/a><\/p>\n<pre><code class=\"python\">import ctypes  lib = ctypes.CDLL('.\/hello.dll')  # Or hello.so if on Linux. hello = lib.hello  hello() <\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u0442\u0438\u043c:<\/p>\n<pre><code class=\"bash\">> python hello.py Hello world! <\/code><\/pre>\n<p>\u041e\u0442\u043b\u0438\u0447\u043d\u043e; \u0442\u0443\u0442 \u0441\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432:<\/p>\n<ol>\n<li>\n<p>Go-\u043a\u043e\u0434 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439, \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435  \u2014 \u043d\u0443\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c <code>\/\/export hello<\/code> \u0434\u043b\u044f \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>hello<\/code> \u0434\u043b\u044f \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f<\/p>\n<\/li>\n<li>\n<p>\u0421\u0431\u043e\u0440\u043a\u0430 \u0441 \u0444\u043b\u0430\u0433\u043e\u043c <code>-buildmode=c-shared<\/code> \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043e\u0431\u0449\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0432 \u0441\u0442\u0438\u043b\u0435 C.<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0442\u0430\u043a\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0432 Python \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 <code>ctypes.CDLL()<\/code><\/p>\n<\/li>\n<\/ol>\n<p><a class=\"anchor\" name=\"2\" id=\"2\"><\/a><\/p>\n<h2>\u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u0432\u043e\u0434 \u0438 \u0432\u044b\u0432\u043e\u0434<\/h2>\n<p>\u0427\u0442\u043e \u0436, \u0442\u0435\u043f\u0435\u0440\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435:<\/p>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/primitive\/primitive.go\" rel=\"noopener noreferrer nofollow\"><strong>primitive.go<\/strong><\/a><\/p>\n<pre><code class=\"go\">\/\/export add func add(a, b int64) int64 {     return a + b } <\/code><\/pre>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/primitive\/primitive.py\" rel=\"noopener noreferrer nofollow\"><strong>primitive.py<\/strong><\/a><\/p>\n<pre><code class=\"python\">lib = ctypes.CDLL('.\/primitive.dll') add = lib.add  # \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 C-\u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 add.argtypes = [ctypes.c_int64, ctypes.c_int64] add.restype = ctypes.c_int64  print('10 + 15 =', add(10, 15)) <\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c:<\/p>\n<pre><code class=\"bash\">> python add.py 10 + 15 = 25 <\/code><\/pre>\n<p>\u0418\u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 Go, \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b <code>argtypes<\/code> \u0438 <code>restype<\/code> \u0438\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <code>ctypes<\/code>. \u041f\u0430\u0440\u0443 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432:<\/p>\n<ol>\n<li>\n<p><code>argtypes<\/code> \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u043f\u0435\u0440\u0435\u0434 \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u0447\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430<\/p>\n<\/li>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u044d\u0442\u0438\u0445 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 Python, \u043a\u0430\u043a \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f Python \u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <code>ctypes<\/code>, \u0438 \u043a\u0430\u043a \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f Python.<\/p>\n<\/li>\n<\/ol>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0438\u0441\u043a\u0430\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u0442\u0438\u043f\u0430\u043c\u0438 C \u0438 \u0442\u0438\u043f\u0430\u043c\u0438 Go \u0432 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435 <code>.h<\/code> \u043f\u043e\u0441\u043b\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0432\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u0434\u0430 Go \u0441 <code>-buildmode=c-shared<\/code>.<\/p>\n<p><strong>Attention: \u0441\u0442\u0440\u043e\u0433\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u0442\u0438\u043f\u044b \u0442\u0435\u0441\u043d\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u044b \u0441 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043e\u0439 \u0436\u0435\u043b\u0435\u0437\u0430. \u0412 \u043e\u0431\u0449\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u044b\u0435 \u0442\u0438\u043f\u044b (<\/strong><code><strong>int64<\/strong><\/code><strong>), \u0447\u0435\u043c \u0431\u0435\u0437\u0440\u0430\u0437\u043c\u0435\u0440\u043d\u044b\u0435 (<\/strong><code><strong>int<\/strong><\/code><strong>)<\/strong><\/p>\n<p><a class=\"anchor\" name=\"3\" id=\"3\"><\/a><\/p>\n<h2>\u041c\u0430\u0441\u0441\u0438\u0432\u044b \u0438 \u0441\u0440\u0435\u0437\u044b<\/h2>\n<p>\u041e\u043a\u0435\u0439, \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u0438 \u0441\u0440\u0435\u0437\u0430\u0445. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043c\u044b \u0432\u0441\u0442\u0443\u043f\u0430\u0435\u043c \u0432 \u0437\u043e\u043d\u0443 \u043d\u0435\u0437\u0430\u0449\u0438\u0449\u0435\u043d\u043d\u043e\u0433\u043e \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u2014 \u0445\u043e\u0442\u044f Python \u0438 Go \u0432 \u0446\u0435\u043b\u043e\u043c \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b \u043f\u043e \u043f\u0430\u043c\u044f\u0442\u0438, \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 \u043d\u0435\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u043f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044e \u0431\u0443\u0444\u0435\u0440\u0430 \u0438\u043b\u0438 \u0443\u0442\u0435\u0447\u043a\u0430\u043c<a href=\"https:\/\/uproger.com\/kak-vyzyvat-funkczii-go-iz-python-s-pomoshhyu-ctypes\/\" rel=\"noopener noreferrer nofollow\">.<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/arrays\/arrays.go\" rel=\"noopener noreferrer nofollow\"><strong>arrays.go<\/strong><\/a><\/p>\n<pre><code class=\"go\">\/\/ \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u044b \u0432\u0432\u0435\u0434\u0451\u043d\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \/\/ \/\/export squares func squares(numsPtr *float64, outPtr *float64, n int64) { nums := unsafe.Slice(numsPtr, n) out := unsafe.Slice(outPtr, n)  \/\/ \u043a\u0441\u0442\u0430\u0442\u0438, \u0434\u043b\u044f Go &lt; 1.17 \/\/ nums := (*[1 &lt;&lt; 30]float64)(unsafe.Pointer(numsPtr))[:n:n] \/\/ out := (*[1 &lt;&lt; 30]float64)(unsafe.Pointer(outPtr))[:n:n]  for i, x := range nums { out[i] = x * x } } <\/code><\/pre>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/arrays\/arrays.py\" rel=\"noopener noreferrer nofollow\"><strong>arrays.py<\/strong><\/a><\/p>\n<p>\u0418 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u044d\u0442\u0443 <code>dll<\/code> \u0432 Python:<\/p>\n<pre><code class=\"python\">lib = ctypes.CDLL('.\/arrays.dll') squares = lib.squares  squares.argtypes = [     ctypes.POINTER(ctypes.c_double),     ctypes.POINTER(ctypes.c_double),     ctypes.c_int64, ]  # \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c from_buffer() \u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e, \u0447\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c: # (ctypes.c_double * 3)(*[1, 2, 3]) nums = array('d', [1, 2, 3]) nums_ptr = (ctypes.c_double * len(nums)).from_buffer(nums) out = array('d', [0, 0, 0]) out_ptr = (ctypes.c_double * len(out)).from_buffer(out)  squares(nums_ptr, out_ptr, len(nums)) print('nums:', list(nums)) print('out:', list(out)) <\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c:<\/p>\n<pre><code class=\"bash\">> python squares.py nums: [1.0, 2.0, 3.0] out: [1.0, 4.0, 9.0] <\/code><\/pre>\n<p>\u0418\u0442\u043e\u0433\u0438: \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043e \u0441\u043f\u0438\u0441\u043a\u0430\u043c\u0438 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u044b C. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>(ctypes.my_type * my_length)(1, 2, 3 ...)<\/code>. \u0411\u043e\u043b\u0435\u0435 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 <code>array<\/code>, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0432\u044b\u0448\u0435. \u0415\u0449\u0451 \u043a\u043e\u0441\u043d\u0451\u043c\u0441\u044f \u044d\u0442\u043e\u0433\u043e \u0447\u0443\u0442\u044c \u043f\u043e\u0437\u0436\u0435, \u043a\u043e\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u043c \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043e \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430\u0445.<\/p>\n<p>\u0412 Go \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u0447\u0442\u043e\u0431\u044b C-\u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b \u043d\u0430 \u0441\u0440\u0435\u0437. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 Go \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0431\u0443\u0444\u0435\u0440\u0430\u043c\u0438 Python.<\/p>\n<p>\u0415\u0449\u0451 \u043f\u0430\u0440\u0430 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c Go \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 CGo, \u044d\u0442\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u0442 \u043a \u043e\u0448\u0438\u0431\u043a\u0435. \u0412\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 C \u0438\u0437 Go \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>C.malloc()<\/code> \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e. \u041e\u0434\u043d\u0430\u043a\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u0431\u043e\u0440\u0449\u0438\u043a \u043c\u0443\u0441\u043e\u0440\u0430 \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u0442 \u0441 \u0442\u0430\u043a\u0438\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0442\u0430\u043a\u0438\u0445 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0443\u0442\u0435\u0447\u043a\u0443 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u0427\u0442\u043e \u0442\u0443\u0442 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u043e\u0432\u0430\u0442\u044c? \u0427\u0442\u043e\u0431\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043c\u043e\u0433\u043b\u0430 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u0437 Go \u043d\u0443\u0436\u043d\u043e \u043b\u0438\u0431\u043e \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0430\u043b\u043b\u043e\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043d\u0438\u0445 \u043f\u0430\u043c\u044f\u0442\u044c \u0432 Python \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0432 Go \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043b\u0438\u0431\u043e \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0432 Go \u0438 \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0432 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 (\u0447\u0443\u0442\u044c \u043a\u043e\u0441\u043d\u0451\u043c\u0441\u044f \u044d\u0442\u043e\u0433\u043e \u0434\u0430\u043b\u044c\u0448\u0435).<\/p>\n<p><strong>\u041f\u043e\u0434\u0432\u0435\u0434\u0451\u043c \u043a\u0440\u0430\u0442\u043a\u043e\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0435\u0439:<\/strong><\/p>\n<ul>\n<li>\n<p>\u0412\u043e\u0437\u0432\u0440\u0430\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439 Go \u0432 Python. <strong>\u041e\u0448\u0438\u0431\u043a\u0430.<\/strong><\/p>\n<\/li>\n<li>\n<p>\u0412\u043e\u0437\u0432\u0440\u0430\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439 C \u0438\u0437 Go \u0432 Python \u0431\u0435\u0437 \u044f\u0432\u043d\u043e\u0433\u043e \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f. <strong>\u0423\u0442\u0435\u0447\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438.<\/strong><\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0442\u0435\u0440\u044f \u0441\u0441\u044b\u043b\u043a\u0438 <code>ctypes<\/code> \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430 Go (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 <code>ctypes.addressof<\/code> \u0438 \u0441\u0431\u0440\u043e\u0441\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430-\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f). <strong>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u0430\u044f \u043e\u0448\u0438\u0431\u043a\u0430 \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438.<\/strong><\/p>\n<\/li>\n<\/ul>\n<p><a class=\"anchor\" name=\"4\" id=\"4\"><\/a><\/p>\n<h2>\u0421\u0442\u0440\u043e\u043a\u0438<\/h2>\n<p>\u0421\u0442\u0440\u043e\u043a\u0438 \u0443\u0441\u0442\u0440\u043e\u0435\u043d\u044b \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043a\u0430\u043a \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0432 \u043f\u043b\u0430\u043d\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u044c\u044e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0441\u0435, \u0447\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438, \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c\u043e \u0438 \u043a \u043d\u0438\u043c. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043e\u0431\u0441\u0443\u0434\u0438\u043c \u043f\u0430\u0440\u0443 \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0445 \u043f\u0440\u0438\u0451\u043c\u043e\u0432 \u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0434\u0432\u043e\u0434\u043d\u044b\u0435 \u043a\u0430\u043c\u043d\u0438:<\/p>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/string\/string.go\" rel=\"noopener noreferrer nofollow\"><strong>string.go<\/strong><\/a><\/p>\n<pre><code class=\"go\">\/\/export repeat func repeat(s *C.char, n int64, out *byte, outN int64) *byte { \/\/ \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u043c \u043d\u0430\u0448 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u0431\u0443\u0444\u0435\u0440 \u0432 \u0431\u0443\u0444\u0435\u0440 Go  outBytes := unsafe.Slice(out, outN)[:0] buf := bytes.NewBuffer(outBytes)  var goString string = C.GoString(s) \/\/ \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u0432\u0432\u043e\u0434 \u0432 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043f\u0430\u043c\u044f\u0442\u0438 Go for i := int64(0); i &lt; n; i++ { buf.WriteString(goString) } buf.WriteByte(0) \/\/ \u0432\u0430\u0436\u043d\u043e - \u043d\u0443\u043b\u0435\u0432\u043e\u0439 \u0431\u0430\u0439\u0442 \u0432 \u043a\u043e\u043d\u0435\u0446 \u0441\u0442\u0440\u043e\u043a\u0438 return out } <\/code><\/pre>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/string\/string.py\" rel=\"noopener noreferrer nofollow\"><strong>string.py<\/strong><\/a><\/p>\n<pre><code class=\"python\">lib = ctypes.CDLL('.\/string.dll') repeat = lib.repeat  repeat.argtypes = [     ctypes.c_char_p,     ctypes.c_int64,     ctypes.c_char_p,     ctypes.c_int64, ] repeat.restype = ctypes.c_char_p  #  buf_size = 1000 buf = ctypes.create_string_buffer(buf_size)  result = repeat(b'Badger', 4, buf, buf_size)  # type(result) = bytes print('Badger * 4 =', result.decode())  result = repeat(b'Snake', 5, buf, buf_size) print('Snake * 5 =', result.decode()) <\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c:<\/p>\n<pre><code class=\"bash\">> python repeat.py Badger * 4 = BadgerBadgerBadgerBadger Snake * 5 = SnakeSnakeSnakeSnakeSnake <\/code><\/pre>\n<p>\u0421\u0442\u0440\u043e\u043a\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u043f\u0443\u0442\u0435\u043c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u043a\u0438 Python \u0432 \u043e\u0431\u044a\u0435\u043a\u0442 <code>bytes<\/code> (\u043e\u0431\u044b\u0447\u043d\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0432\u044b\u0437\u043e\u0432\u0430 <code>encode()<\/code>), \u0437\u0430\u0442\u0435\u043c \u0432 C-\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0438 \u0437\u0430\u0442\u0435\u043c \u0432 Go-\u0441\u0442\u0440\u043e\u043a\u0443.<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>ctypes.c_char_p<\/code> \u0432 <code>argtypes<\/code> \u0437\u0430\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 Python \u043e\u0436\u0438\u0434\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 <code>bytes<\/code> \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 C <code>*char<\/code>. \u0412 <code>restype<\/code> \u043e\u043d \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 <code>*char<\/code> \u0432 \u043e\u0431\u044a\u0435\u043a\u0442 <code>bytes<\/code>.<\/p>\n<p>\u0412 Go \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c <code>*char<\/code> \u0432 \u0441\u0442\u0440\u043e\u043a\u0443 Go \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>C.GoString<\/code>. \u042d\u0442\u043e \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043d\u043e\u0432\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443, \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c\u0443\u044e Go \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0441\u0431\u043e\u0440\u043a\u0438 \u043c\u0443\u0441\u043e\u0440\u0430. \u0427\u0442\u043e\u0431\u044b \u0441\u043e\u0437\u0434\u0430\u0442\u044c <code>*char<\/code> \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c <code>C.CString<\/code>. \u041e\u0434\u043d\u0430\u043a\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0442\u0435\u0440\u044f\u043d, \u0435\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u0435 \u043d\u0430 \u043d\u0435\u0433\u043e \u0441\u0441\u044b\u043b\u043a\u0443, \u0438 \u0442\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0435\u0442 \u0443\u0442\u0435\u0447\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438. \u0427\u0442\u043e\u0431\u044b \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u0437 Go, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0435 \u0436\u0435 \u043f\u0440\u0438\u0435\u043c\u044b, \u0447\u0442\u043e \u0438 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0432\u044b\u0432\u043e\u0434 \u0431\u044b\u043b \u043f\u0435\u0440\u0435\u0434\u0430\u043d Python, Go \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e, \u0438 Python \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u0441\u0442 \u0438\u0437 \u043d\u0435\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442 bytes.<\/p>\n<p><strong>\u0418\u0442\u0430\u043a, \u043a\u0430\u043a\u0438\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043c\u043e\u0433\u0443\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c?<\/strong><\/p>\n<ul>\n<li>\n<p>\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 <code>C.CString<\/code> \u0431\u0435\u0437 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u0441\u044b\u043b\u043a\u0438 \u0434\u043b\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f. <strong>\u0423\u0442\u0435\u0447\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438.<\/strong><\/p>\n<\/li>\n<li>\n<p>\u041d\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u0431\u0430\u0439\u0442\u0430 \u0432 \u043a\u043e\u043d\u0435\u0446 \u0432\u044b\u0432\u043e\u0434\u0438\u043c\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438. <strong>\u041f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0431\u0443\u0444\u0435\u0440\u0430 \u043f\u0440\u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0432 \u043e\u0431\u044a\u0435\u043a\u0442 Python.<\/strong><\/p>\n<\/li>\n<li>\n<p>\u041e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0431\u0443\u0444\u0435\u0440\u0430 \u0432 Go. <strong>\u041f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0431\u0443\u0444\u0435\u0440\u0430 \u0438\u043b\u0438 \u043d\u0435\u043f\u043e\u043b\u043d\u044b\u0439 \u0432\u044b\u0432\u043e\u0434.<\/strong><\/p>\n<\/li>\n<\/ul>\n<p><a class=\"anchor\" name=\"5\" id=\"5\"><\/a><\/p>\n<h2>\u041c\u0430\u0441\u0441\u0438\u0432 \u0441\u0442\u0440\u043e\u043a<\/h2>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u0442\u0440\u043e\u043a \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a:<\/p>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/join\/join.go\" rel=\"noopener noreferrer nofollow\"><strong>join.go<\/strong><\/a><\/p>\n<pre><code class=\"go\">func goStrings(cstrs **C.char) []string { var result []string slice := unsafe.Slice(cstrs, 1&lt;&lt;30) for i := 0; slice[i] != nil; i++ { result = append(result, C.GoString(slice[i])) } return result } <\/code><\/pre>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/join\/join.py\" rel=\"noopener noreferrer nofollow\"><strong>join.py<\/strong><\/a><\/p>\n<pre><code class=\"python\">def to_c_str_array(strs: List[str]):     ptr = (ctypes.c_char_p * (len(strs) + 1))()     ptr[:-1] = [s.encode() for s in strs]     ptr[-1] = None       return ptr <\/code><\/pre>\n<p><a class=\"anchor\" name=\"6\" id=\"6\"><\/a><\/p>\n<h2>Numpy \u0438 Pandas<\/h2>\n<p>\u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0431\u0443\u0444\u0435\u0440\u0430\u043c NumPy \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0430 <code>.ctypes.data_as(ctypes.whatever)<\/code>. \u0412 pandas \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0442\u0440\u0438\u0431\u0443\u0442 <code>.values<\/code> \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 NumPy, \u0430 \u0437\u0430\u0442\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 NumPy \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043c\u043e\u0436\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\/\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043d\u0430 \u043c\u0435\u0441\u0442\u0435, \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0430\u043f\u043e\u0434\u043e\u0431\u0438\u0435:<\/p>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/numpypandas\/numpypandas.go\" rel=\"noopener noreferrer nofollow\"><strong>numpypandas.go<\/strong><\/a><\/p>\n<pre><code class=\"go\">\/\/export increase func increase(numsPtr *int64, n int64, a int64) { nums := unsafe.Slice(numsPtr, n) for i := range nums { nums[i] += a } } <\/code><\/pre>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/numpypandas\/numpypandas.py\" rel=\"noopener noreferrer nofollow\"><strong>numpypandas.py<\/strong><\/a><\/p>\n<pre><code class=\"python\">lib = ctypes.CDLL('.\/numpypandas.dll') increase = lib.increase  increase.argtypes = [     ctypes.POINTER(ctypes.c_int64),     ctypes.c_int64,     ctypes.c_int64, ]  people = pandas.DataFrame({     'name': ['Alice', 'Bob', 'Charlie'],     'age': [20, 30, 40], })  # \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0442\u0438\u043f ages = people.age if str(ages.dtypes) != 'int64':     raise TypeError(f'Expected type int64, got {ages.dtypes}')  values = ages.values  # type=numpy.Array ptr = values.ctypes.data_as(ctypes.POINTER(ctypes.c_int64))  print('Before') print(people)  print('After') increase(ptr, len(people), 5) print(people) <\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c:<\/p>\n<pre><code class=\"bash\">> python table.py Before       name  age 0    Alice   20 1      Bob   30 2  Charlie   40 After       name  age 0    Alice   25 1      Bob   35 2  Charlie   45 > <\/code><\/pre>\n<p>\u0412\u0430\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0442\u0438\u043f \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e Go, \u0432\u0435\u0434\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u043e\u0432\u043e\u0433\u043e \u0442\u0438\u043f\u0430 (<code>int<\/code>&lt;-><code>float<\/code>), \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 (<code>int64<\/code>&lt;-><code>int32<\/code>) \u0438\u043b\u0438 \u0442\u0438\u043f\u0430 \u0432\u043e\u043e\u0431\u0449\u0435 <code>object<\/code>.<\/p>\n<p>\u0415\u0449\u0435 \u043e\u0434\u0438\u043d \u043c\u043e\u043c\u0435\u043d\u0442, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u2014 Pandas \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u0440\u0438 \u0432\u044b\u0431\u043e\u0440\u0435 \u0441\u0442\u0440\u043e\u043a. \u0421\u043a\u0430\u0436\u0435\u043c, \u0435\u0441\u043b\u0438 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c <code>DataFrame<\/code> \u0441 \u0438\u043c\u0435\u043d\u0435\u043c <code>people<\/code>, \u0442\u043e <code>people[people['age'] &lt; 40]<\/code> \u0432\u0435\u0440\u043d\u0435\u0442 \u043a\u043e\u043f\u0438\u044e <code>people<\/code>. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u043a\u043e\u043f\u0438\u0438 \u0432 Go \u043d\u0435 \u043f\u043e\u0432\u043b\u0438\u044f\u0435\u0442 \u043d\u0430 \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443.<\/p>\n<p><a class=\"anchor\" name=\"7\" id=\"7\"><\/a><\/p>\n<h2>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b<\/h2>\n<p>\u0427\u0442\u043e\u0431\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0438\u0445 \u043a\u0430\u043a \u0432 Python, \u0442\u0430\u043a \u0438 \u0432 C. \u042d\u043a\u0441\u043f\u043e\u0440\u0442 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 Go \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u0435\u043d.<\/p>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/structs\/structs.go\" rel=\"noopener noreferrer nofollow\"><strong>structs.go<\/strong><\/a><\/p>\n<pre><code class=\"go\">\/* struct person {   char* firstName;   char* lastName;   char* fullName;   long long fullNameLen; }; *\/ import \"C\" import ( \"bytes\" \"unsafe\" )  \/\/export fill func fill(p *C.struct_person) { buf := bytes.NewBuffer(unsafe.Slice((*byte)(unsafe.Pointer(p.fullName)), p.fullNameLen)[:0]) first := C.GoString(p.firstName) last := C.GoString(p.lastName) buf.WriteString(first + \" \" + last) buf.WriteByte(0) } <\/code><\/pre>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/structs\/structs.py\" rel=\"noopener noreferrer nofollow\"><strong>structs.py<\/strong><\/a><\/p>\n<pre><code class=\"python\">class Person(ctypes.Structure):     _fields_ = [         ('first_name', ctypes.c_char_p),         ('last_name', ctypes.c_char_p),         ('full_name', ctypes.c_char_p),         ('full_name_len', ctypes.c_int64),     ]   lib = ctypes.CDLL('.\/structs.dll')  fill = lib.fill fill.argtypes = [ctypes.POINTER(Person)]  buf_size = 1000 buf = ctypes.create_string_buffer(buf_size) person = Person(b'John', b'Galt', buf.value, len(buf)) fill(ctypes.pointer(person))  print(person.full_name) <\/code><\/pre>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b Go, \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0438\u0445 \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 C, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044f \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439 \u043d\u0430\u0434 \u0441\u0442\u0440\u043e\u043a\u043e\u0439 <code>import \"C\"<\/code>. \u041a\u0441\u0442\u0430\u0442\u0438, \u043a\u0430\u043a \u0432\u0438\u0434\u043d\u043e, \u0432 Go \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 <code>person<\/code> \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a <code>C.struct_person<\/code>. \u0412 Python \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 <code>ctypes.Structure<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u043c\u0435\u0435\u0442 \u0442\u043e\u0447\u043d\u043e \u0442\u0430\u043a\u0438\u0435 \u0436\u0435 \u043f\u043e\u043b\u044f.<\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043f\u043e\u043b\u044f <code>struct<\/code> \u0432 Go, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u043f\u0440\u0438\u043c\u0438\u0442\u0438\u0432\u044b. \u0415\u0441\u043b\u0438 \u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0438 \u0441\u0442\u0440\u043e\u043a\u0438, \u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0442 \u0442\u0435 \u0436\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f, \u0447\u0442\u043e \u0438 \u0440\u0430\u043d\u044c\u0448\u0435.<\/p>\n<p><a class=\"anchor\" name=\"8\" id=\"8\"><\/a><\/p>\n<h2>\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 __del__<\/h2>\n<p>\u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0443\u0434\u043e\u0431\u043d\u043e\u0439 \u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0439 \u0441\u0445\u0435\u043c\u044b \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u2014 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435, \u0447\u0442\u043e \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <a href=\"https:\/\/docs.python.org\/3\/reference\/datamodel.html#object.__del__\" rel=\"noopener noreferrer nofollow\">\u0434\u0430\u043d\u0434\u0435\u0440-\u043c\u0435\u0442\u043e\u0434 Python (<code>__del__<\/code>)<\/a>, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0443\u0434\u043e\u0431\u043d\u043e \u0430\u043b\u043b\u043e\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c \u043f\u043e\u0434 \u0431\u0443\u0444\u0435\u0440\u044b \u0432 Go (C), \u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0442\u044c \u0435\u0451 \u0432 Python, \u043a\u043e\u0433\u0434\u0430 \u043e\u0431\u044a\u0435\u043a\u0442 \u0443\u0434\u0430\u043b\u044f\u0435\u0442\u0441\u044f.<\/p>\n<p>\u042d\u0442\u0430 \u0441\u0445\u0435\u043c\u0430 \u043f\u0440\u043e\u0441\u0442\u0430 \u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 2 \u0432\u0435\u0449\u0435\u0439: \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 Go, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0434\u0435\u0430\u043b\u043b\u043e\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c \u0434\u043b\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 Python, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e Go.<\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f Python \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438, \u043a\u043e\u0433\u0434\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0441\u044b\u043b\u043e\u043a \u043d\u0430 \u043e\u0431\u044a\u0435\u043a\u0442 \u0441\u0442\u0430\u043d\u0435\u0442 \u0440\u0430\u0432\u043d\u044b\u043c \u043d\u0443\u043b\u044e.<\/p>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/del\/del.go\" rel=\"noopener noreferrer nofollow\"><strong>del.go<\/strong><\/a><\/p>\n<pre><code class=\"go\">\/* #include &lt;stdlib.h> struct userInfo {   char* info; }; *\/ import \"C\" import ( \"fmt\" \"unsafe\" )  \/\/ \u0430\u043b\u043b\u043e\u0446\u0438\u0440\u0443\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u044c \u0434\u043b\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \/\/ \/\/export getUserInfo func getUserInfo(cname *C.char) C.struct_userInfo { var result C.struct_userInfo name := C.GoString(cname) result.info = C.CString( fmt.Sprintf(\"User %q has %v letters in their name\", name, len(name))) return result }  \/\/ \u0434\u0435\u0430\u043b\u043b\u043e\u0446\u0438\u0440\u0443\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u044c \u0434\u043b\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \/\/ \/\/export delUserInfo func delUserInfo(info C.struct_userInfo) { \/\/ \u043f\u0435\u0447\u0430\u0442\u0430\u0435\u043c \u0434\u043b\u044f \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u0441\u0442\u0438 fmt.Printf(\"Freeing user info: %s\\n\", C.GoString(info.info)) C.free(unsafe.Pointer(info.info)) } <\/code><\/pre>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/del\/del.py\" rel=\"noopener noreferrer nofollow\"><strong>del.py<\/strong><\/a><\/p>\n<pre><code class=\"python\">class UserInfo(ctypes.Structure):     _fields_ = [('info', ctypes.c_char_p)]      def __del__(self):         del_user_info(self)  lib = ctypes.CDLL('del.dll') get_user_info = lib.getUserInfo get_user_info.argtypes = [ctypes.c_char_p] get_user_info.restype = UserInfo del_user_info = lib.delUserInfo del_user_info.argtypes = [UserInfo]  def work_work():     user1 = get_user_info('Alice'.encode())     print('Info:', user1.info.decode())     print('-----------')      user2 = get_user_info('Bob'.encode())     print('Info:', user2.info.decode())     print('-----------')      # \u0412 \u044d\u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u043e\u0431\u044a\u0435\u043a\u0442\u044b user1 \u0438 user2 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u044b  work_work() print('Did I remember to free my memory?') <\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c:<\/p>\n<pre><code class=\"bash\">Name: Alice Description: User \"Alice\" has 5 letters in their name Name length: 5 ----------- Name: Bob Description: User \"Bob\" has 3 letters in their name Name length: 3 ----------- Freeing user info: User \"Alice\" has 5 letters in their name Freeing user info: User \"Bob\" has 3 letters in their name Did I remember to free my memory? <\/code><\/pre>\n<p>\u0412\u0435\u043b\u0438\u043a\u043e\u043b\u0435\u043f\u043d\u043e<\/p>\n<p><a class=\"anchor\" name=\"9\" id=\"9\"><\/a><\/p>\n<h2>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a<\/h2>\n<p>\u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u043e\u0448\u0438\u0431\u043e\u043a Go \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 Python \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430 \u0434\u043b\u044f \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0439 \u0442\u0438\u043f \u043e\u0448\u0438\u0431\u043a\u0438.<\/p>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/error\/error.go\" rel=\"noopener noreferrer nofollow\"><strong>error.go<\/strong><\/a><\/p>\n<pre><code class=\"go\">\/* #include &lt;stdlib.h> typedef struct { char* err; } error; *\/ import \"C\"  \/\/ ...  func newError(s string, args ...interface{}) C.error { if s == \"\" { return C.error{}  \/\/ \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e \u043e\u0448\u0438\u0431\u043a\u0435 nil \u0432 Go  } msg := fmt.Sprintf(s, args...) return C.error{C.CString(msg)} }  \/\/export delError func delError(err C.error) { if err.err == nil { return } C.free(unsafe.Pointer(err.err)) } <\/code><\/pre>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/error\/error.py\" rel=\"noopener noreferrer nofollow\"><strong>error.py<\/strong><\/a><\/p>\n<pre><code class=\"python\">class Error(ctypes.Structure):     _fields_ = [('err', ctypes.c_char_p)]      def __del__(self):         if self.err is not None:             del_error(self)      def raise_if_err(self):         if self.err is not None:             raise IOError(self.err.decode())  # ...  del_error = lib.delError del_error.argtypes = [Error] <\/code><\/pre>\n<p>\u041e\u0442\u043b\u0438\u0447\u043d\u043e, \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0442\u0438\u043f <code>Error<\/code> \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u0445 \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u0445 \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438<\/p>\n<p><a class=\"anchor\" name=\"10\" id=\"10\"><\/a><\/p>\n<h2>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043e \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438<\/h2>\n<p><a class=\"anchor\" name=\"11\" id=\"11\"><\/a><\/p>\n<h3>\u0421\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430<\/h3>\n<p>\u0421\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u2014 \u0432 \u0440\u0430\u0439\u043e\u043d\u0435 5 \u043c\u043a\u0441. \u041d\u0435\u043c\u0430\u043b\u043e, \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u043d\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e <a href=\"https:\/\/stackoverflow.com\/questions\/28272285\/why-cgos-performance-is-so-slow-is-there-something-wrong-with-my-testing-code\" rel=\"noopener noreferrer nofollow\">CGo \u0438\u043c\u0435\u0435\u0442 \u0432\u044b\u0441\u043e\u043a\u0438\u0435 \u043d\u0430\u043a\u043b\u0430\u0434\u043d\u044b\u0435 \u0440\u0430\u0441\u0445\u043e\u0434\u044b \u043d\u0430 \u0432\u044b\u0437\u043e\u0432<\/a>. \u041f\u0440\u0438\u0447\u0451\u043c \u044d\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0438 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 Go \u0438\u0437 \u043d\u0430\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u043d\u0430 \u0421\u0438, \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u043e\u0442 \u0442\u043e\u0433\u043e, \u0441\u0432\u044f\u0437\u0430\u043d \u043b\u0438 \u043a\u043e\u0434 Go \u0447\u0435\u0440\u0435\u0437 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0438\u043b\u0438 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443.<\/p>\n<p>\u042d\u0442\u0438 \u043d\u0430\u043a\u043b\u0430\u0434\u043d\u044b\u0435 \u0440\u0430\u0441\u0445\u043e\u0434\u044b \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 API. \u0415\u0441\u043b\u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f 5 \u043c\u043a\u0441 \u0440\u0430\u0431\u043e\u0442\u044b Go, \u0442\u043e \u043d\u0430 \u043d\u0430\u043a\u043b\u0430\u0434\u043d\u044b\u0435 \u0440\u0430\u0441\u0445\u043e\u0434\u044b \u043d\u0430 \u0432\u044b\u0437\u043e\u0432 \u0431\u0443\u0434\u0435\u0442 \u0442\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f 50% \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u0415\u0441\u043b\u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f 500 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 Go, \u0442\u043e \u043d\u0430\u043a\u043b\u0430\u0434\u043d\u044b\u0435 \u0440\u0430\u0441\u0445\u043e\u0434\u044b \u043d\u0430 \u0432\u044b\u0437\u043e\u0432\u044b \u0441\u043e\u0441\u0442\u0430\u0432\u044f\u0442 \u043e\u043a\u043e\u043b\u043e 1%.<\/p>\n<p><a class=\"anchor\" name=\"12\" id=\"12\"><\/a><\/p>\n<h3>\u041f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438<\/h3>\n<p>\u0414\u043b\u044f \u0432\u044b\u0437\u043e\u0432\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0442\u0441\u044f \u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0437, \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u0438\u043c\u0435\u0435\u0442 \u0441\u043c\u044b\u0441\u043b \u043c\u043e\u0436\u043d\u043e \u0430\u043b\u043b\u043e\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c 1 \u0440\u0430\u0437 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>ctypes<\/code> \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0451 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u0432\u044b\u0437\u043e\u0432\u043e\u0432.<\/p>\n<p>\u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u044d\u0442\u043e \u043d\u0430\u043f\u043e\u0434\u043e\u0431\u0438\u0435:<\/p>\n<pre><code class=\"python\"># \u043e\u0431\u0451\u0440\u0442\u043a\u0430 ctypes \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 Go my_function = my_lib.my_function  def my_function_with_buffer(n: int):     buffer = (ctypes.c_char * n)(*([0] * n))     def my_function_with_closure():         my_function(buffer, n)     return my_function_with_closure  def work_work():     my_function_buffered = my_function_with_buffer(1000)     my_function_buffered()     my_function_buffered()     my_function_buffered() <\/code><\/pre>\n<p><a class=\"anchor\" name=\"13\" id=\"13\"><\/a><\/p>\n<h3>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 array \u0434\u043b\u044f \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0438 \u043f\u0430\u043c\u044f\u0442\u0438<\/h3>\n<p>\u041a\u0430\u043a \u0443\u0436\u0435 \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u043b\u043e\u0441\u044c \u0432\u044b\u0448\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <code>array<\/code> \u0434\u043b\u044f \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <code>(ctypes.type * n)<\/code>.<\/p>\n<p><a class=\"anchor\" name=\"14\" id=\"14\"><\/a><\/p>\n<h2>\u0411\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0438<\/h2>\n<p>\u041d\u0443 \u0438 \u043d\u0430\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u043a \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0439, \u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0438\u0440\u0443\u044e\u0449\u0438\u0445 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e \u043e\u0442 \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 Go \u0438\u0437 Python \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0439 Python. \u0414\u043b\u044f \u043f\u043e\u043b\u043d\u043e\u0442\u044b \u043a\u0430\u0440\u0442\u0438\u043d\u044b \u0432\u0441\u0435 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044f \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0442 \u043d\u0430\u043a\u043b\u0430\u0434\u043d\u044b\u0435 \u0440\u0430\u0441\u0445\u043e\u0434\u044b \u043d\u0430 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0432 C-\u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e.<\/p>\n<p><a class=\"anchor\" name=\"15\" id=\"15\"><\/a><\/p>\n<h3>\u0412\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u03c0<\/h3>\n<p>\u041f\u0440\u043e\u0441\u0442\u043e\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u043b\u0430 \u03c0, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e \u0442\u043e\u043c, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c Go.<\/p>\n<figure class=\"\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/7d8\/a68\/9de\/7d8a689defddbc3d3e73a90b922b9ca1.png\" alt=\"pi\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/7d8\/a68\/9de\/7d8a689defddbc3d3e73a90b922b9ca1.png\"\/><\/p>\n<div><figcaption>pi<\/figcaption><\/div>\n<\/figure>\n<p><a class=\"anchor\" name=\"16\" id=\"16\"><\/a><\/p>\n<h3>\u041f\u0435\u0440\u0435\u043c\u0435\u0448\u0438\u0432\u0430\u043d\u0438\u0435 10M \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435<\/h3>\n<p>\u0425\u043c\u043c, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 Go \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0434\u0430\u0436\u0435 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0445 \u043c\u043e\u0434\u0443\u043b\u0435\u0439 Python.<\/p>\n<figure class=\"\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/acb\/42a\/db5\/acb42adb517174adf04ac6ad0a3a28b0.png\" alt=\"shuffle\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/acb\/42a\/db5\/acb42adb517174adf04ac6ad0a3a28b0.png\"\/><\/p>\n<div><figcaption>shuffle<\/figcaption><\/div>\n<\/figure>\n<p><a class=\"anchor\" name=\"17\" id=\"17\"><\/a><\/p>\n<h3>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 array \u0438 \u043c\u0435\u0442\u043e\u0434, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u043e\u0432\u0430\u043d\u043d\u044b\u0439 ctypes<\/h3>\n<p>\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u0430, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e <code>ctypes<\/code>, \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <code>array<\/code> \u0434\u043b\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 Python \u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f C.<\/p>\n<pre><code class=\"python\"># \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c ctypes cvals = (ctypes.c_double * n)(*nums)  # \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c array arr = array('d', nums) cvals = (ctypes.c_double * n).from_buffer(arr) <\/code><\/pre>\n<figure class=\"\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/post_images\/68a\/6b8\/9fd\/68a6b89fd504e20274d539318fb18aba.png\" alt=\"list\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/68a\/6b8\/9fd\/68a6b89fd504e20274d539318fb18aba.png\"\/><\/p>\n<div><figcaption>list<\/figcaption><\/div>\n<\/figure>\n<p>\u0427\u0442\u043e \u0436, \u0432\u043e\u0442 \u043c\u044b \u0438 \u043e\u0431\u0441\u0443\u0434\u0438\u043b\u0438, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c Go \u0438\u0437 Python, \u0441\u043f\u0430\u0441\u0438\u0431\u043e \u0413\u0432\u0438\u0434\u043e \u0437\u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0438\u0448\u043d\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0432 Python<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0431\u044b\u043b\u0438 \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u043d\u0435\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u2014 \u043f\u043e\u043f\u0440\u0430\u0432\u044c\u0442\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0445<\/p>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, \u044f \u0432\u0435\u0434\u0443 <a href=\"https:\/\/t.me\/+jTQc-QM7D5YxNjgy\" rel=\"noopener noreferrer nofollow\">\u0442\u0435\u043b\u0435\u0433\u0440\u0430\u043c-\u043a\u0430\u043d\u0430\u043b<\/a> \u043f\u043e Python, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0438, \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438, open-source \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0438 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e<br \/> , \u0430 \u0442\u0435\u043c, \u043a\u0442\u043e \u043b\u044e\u0431\u0438\u0442 \u0438 \u0438\u0437\u0443\u0447\u0430\u0435\u0442 <a href=\"https:\/\/t.me\/+m5Fkp2kY7Xw3ZjMy\" rel=\"noopener noreferrer nofollow\">Golang<\/a>, \u043c\u043e\u0433\u0443 \u043f\u043e\u0441\u043e\u0432\u0435\u0442\u043e\u0432\u0430\u0442\u044c \u0434\u0440\u0443\u0433\u043e\u0439 \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0439 \u0440\u0435\u0441\u0443\u0440\u0441. \u0412\u0435\u0440\u043e\u044f\u0442\u043d\u043e, \u0442\u0430\u043c \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u043d\u0430\u0439\u0442\u0438 \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435 \u0434\u043b\u044f \u0441\u0435\u0431\u044f, \u0442\u0430\u043a \u0447\u0442\u043e welcome)<\/p>\n<p>\u0411\u043e\u043b\u044c\u0448\u043e\u0435 \u0441\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u0435 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438!<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/821007\/\"> https:\/\/habr.com\/ru\/articles\/821007\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u0443\u044e \u043d\u0430 Go \u0438\u0437 Python. \u0417\u0430\u0447\u0435\u043c? \u041f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u043d\u0430 Python \u0438\u043d\u043e\u0433\u0434\u0430 \u0438\u043c\u0435\u0435\u0442 \u0441\u043c\u044b\u0441\u043b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0430 \u0441\u0442\u0430\u0442\u0438\u0447\u043d\u043e\u043c, \u0432\u044b\u0441\u043e\u043a\u043e\u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u043c \u044f\u0437\u044b\u043a\u0435. Go \u043c\u043e\u0436\u0435\u0442 \u0441\u0442\u0430\u0442\u044c \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u043c \u0432\u044b\u0431\u043e\u0440\u043e\u043c \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d \u0431\u044b\u0441\u0442\u0440\u044b\u0439, \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0438 \u043a\u0440\u043e\u0441\u0441\u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435\u043d\u043d\u044b\u0439. \u041d\u0435\u0434\u0430\u0432\u043d\u043e \u0432 \u043c\u043e\u0435\u043c <a href=\"https:\/\/t.me\/+jTQc-QM7D5YxNjgy\" rel=\"noopener noreferrer nofollow\">Python \u043a\u0430\u043d\u0430\u043b\u0435<\/a>, \u043c\u044b \u043e\u0431\u0441\u0443\u0436\u0434\u0430\u043b\u0438, \u043a\u0430\u043a \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0440\u043e\u0434\u0438\u043b\u0430\u0441\u044c \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f.<\/p>\n<p>\u0414\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u0441\u0435\u0445 \u0448\u0430\u0433\u043e\u0432 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f: Python, \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 Go \u0438 GCC (MinGW \u0434\u043b\u044f Windows). \u041f\u0440\u0438\u043c\u0435\u0440\u044b \u043a\u043e\u0434\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0432 \u0440\u0435\u043f\u043e \u043d\u0430 <a href=\"https:\/\/www.github.com\/fluhus\/snopher\" rel=\"noopener noreferrer nofollow\">Github<\/a>.<\/p>\n<p>\u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0432\u044b\u0437\u043e\u0432\u0430 Go \u0438\u0437 Python \u2014 <a href=\"http:\/\/swig.org\/\" rel=\"noopener noreferrer nofollow\">SWIG<\/a>, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440. \u0417\u0434\u0435\u0441\u044c \u0436\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c <code>ctypes<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u0438 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442.<\/p>\n<p>\u041f\u043e\u0435\u0445\u0430\u043b\u0438!<\/p>\n<h2>\u041f\u043b\u0430\u043d<\/h2>\n<ul>\n<li>\n<p><a href=\"#1\" rel=\"noopener noreferrer nofollow\">Hello world<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#2\" rel=\"noopener noreferrer nofollow\">\u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u0432\u043e\u0434 \u0438 \u0432\u044b\u0432\u043e\u0434<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#3\" rel=\"noopener noreferrer nofollow\">\u041c\u0430\u0441\u0441\u0438\u0432\u044b \u0438 \u0441\u0440\u0435\u0437\u044b<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#4\" rel=\"noopener noreferrer nofollow\">\u0421\u0442\u0440\u043e\u043a\u0438<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#5\" rel=\"noopener noreferrer nofollow\">\u041c\u0430\u0441\u0441\u0438\u0432 \u0441\u0442\u0440\u043e\u043a<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#6\" rel=\"noopener noreferrer nofollow\">Numpy \u0438 Pandas<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#7\" rel=\"noopener noreferrer nofollow\">\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#8\" rel=\"noopener noreferrer nofollow\">\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 <code>__del__<\/code><\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#9\" rel=\"noopener noreferrer nofollow\">\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0448\u0438\u0431\u043e\u043a<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#10\" rel=\"noopener noreferrer nofollow\">\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043e \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438<\/a><\/p>\n<ul>\n<li>\n<p><a href=\"#11\" rel=\"noopener noreferrer nofollow\">\u0421\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#12\" rel=\"noopener noreferrer nofollow\">\u041f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#13\" rel=\"noopener noreferrer nofollow\">\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <code>array<\/code> \u0434\u043b\u044f \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0438 \u043f\u0430\u043c\u044f\u0442\u0438<\/a><\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><a href=\"#14\" rel=\"noopener noreferrer nofollow\">\u0411\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0438<\/a><\/p>\n<ul>\n<li>\n<p><a href=\"#15\" rel=\"noopener noreferrer nofollow\">\u0412\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u03c0<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#16\" rel=\"noopener noreferrer nofollow\">\u041f\u0435\u0440\u0435\u043c\u0435\u0448\u0438\u0432\u0430\u043d\u0438\u0435 10M \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#17\" rel=\"noopener noreferrer nofollow\">\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>array<\/code> \u0438 \u043c\u0435\u0442\u043e\u0434, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u043e\u0432\u0430\u043d\u043d\u044b\u0439 <code>ctypes<\/code><\/a><\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><a class=\"anchor\" name=\"1\" id=\"1\"><\/a><\/p>\n<h2>Hello world<\/h2>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 hello-world, \u043a\u0443\u0434\u0430 \u0436\u0435 \u0431\u0435\u0437 \u043d\u0435\u0433\u043e<\/p>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/hello\/hello.go\" rel=\"noopener noreferrer nofollow\"><strong>hello.go<\/strong><\/a><\/p>\n<pre><code class=\"go\">package main  import \"C\" import \"fmt\"  \/\/export hello func hello() {     fmt.Println(\"Hello world!\") }  func main() {} <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 <code>hello.go<\/code> \u0444\u0430\u0439\u043b <code>hello.dll<\/code>  \u2014 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c \u043f\u0435\u0440\u0432\u044b\u0439 \u0441 \u0444\u043b\u0430\u0433\u043e\u043c <code>-buildmode=c-shared<\/code><\/p>\n<pre><code class=\"bash\"># Windows: go build -o hello.dll -buildmode=c-shared hello.go # Linux: go build -o hello.so -buildmode=c-shared hello.go <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f <code>hello.dll<\/code> \u0432 Python \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0444\u0430\u0439\u043b, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <code>ctypes.CDLL()<\/code>:<\/p>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/hello\/hello.py\" rel=\"noopener noreferrer nofollow\"><strong>hello.py<\/strong><\/a><\/p>\n<pre><code class=\"python\">import ctypes  lib = ctypes.CDLL('.\/hello.dll')  # Or hello.so if on Linux. hello = lib.hello  hello() <\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u0442\u0438\u043c:<\/p>\n<pre><code class=\"bash\">> python hello.py Hello world! <\/code><\/pre>\n<p>\u041e\u0442\u043b\u0438\u0447\u043d\u043e; \u0442\u0443\u0442 \u0441\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432:<\/p>\n<ol>\n<li>\n<p>Go-\u043a\u043e\u0434 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439, \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435  \u2014 \u043d\u0443\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c <code>\/\/export hello<\/code> \u0434\u043b\u044f \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>hello<\/code> \u0434\u043b\u044f \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f<\/p>\n<\/li>\n<li>\n<p>\u0421\u0431\u043e\u0440\u043a\u0430 \u0441 \u0444\u043b\u0430\u0433\u043e\u043c <code>-buildmode=c-shared<\/code> \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043e\u0431\u0449\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0432 \u0441\u0442\u0438\u043b\u0435 C.<\/p>\n<\/li>\n<li>\n<p>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0442\u0430\u043a\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0432 Python \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 <code>ctypes.CDLL()<\/code><\/p>\n<\/li>\n<\/ol>\n<p><a class=\"anchor\" name=\"2\" id=\"2\"><\/a><\/p>\n<h2>\u041f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u0432\u043e\u0434 \u0438 \u0432\u044b\u0432\u043e\u0434<\/h2>\n<p>\u0427\u0442\u043e \u0436, \u0442\u0435\u043f\u0435\u0440\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435:<\/p>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/primitive\/primitive.go\" rel=\"noopener noreferrer nofollow\"><strong>primitive.go<\/strong><\/a><\/p>\n<pre><code class=\"go\">\/\/export add func add(a, b int64) int64 {     return a + b } <\/code><\/pre>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/primitive\/primitive.py\" rel=\"noopener noreferrer nofollow\"><strong>primitive.py<\/strong><\/a><\/p>\n<pre><code class=\"python\">lib = ctypes.CDLL('.\/primitive.dll') add = lib.add  # \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 C-\u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 add.argtypes = [ctypes.c_int64, ctypes.c_int64] add.restype = ctypes.c_int64  print('10 + 15 =', add(10, 15)) <\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c:<\/p>\n<pre><code class=\"bash\">> python add.py 10 + 15 = 25 <\/code><\/pre>\n<p>\u0418\u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 Go, \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b <code>argtypes<\/code> \u0438 <code>restype<\/code> \u0438\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <code>ctypes<\/code>. \u041f\u0430\u0440\u0443 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432:<\/p>\n<ol>\n<li>\n<p><code>argtypes<\/code> \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u043f\u0435\u0440\u0435\u0434 \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u0447\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430<\/p>\n<\/li>\n<li>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u044d\u0442\u0438\u0445 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 Python, \u043a\u0430\u043a \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f Python \u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <code>ctypes<\/code>, \u0438 \u043a\u0430\u043a \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f Python.<\/p>\n<\/li>\n<\/ol>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0438\u0441\u043a\u0430\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u0442\u0438\u043f\u0430\u043c\u0438 C \u0438 \u0442\u0438\u043f\u0430\u043c\u0438 Go \u0432 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435 <code>.h<\/code> \u043f\u043e\u0441\u043b\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438 \u0432\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u0434\u0430 Go \u0441 <code>-buildmode=c-shared<\/code>.<\/p>\n<p><strong>Attention: \u0441\u0442\u0440\u043e\u0433\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u0442\u0438\u043f\u044b \u0442\u0435\u0441\u043d\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u044b \u0441 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043e\u0439 \u0436\u0435\u043b\u0435\u0437\u0430. \u0412 \u043e\u0431\u0449\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u044b\u0435 \u0442\u0438\u043f\u044b (<\/strong><code><strong>int64<\/strong><\/code><strong>), \u0447\u0435\u043c \u0431\u0435\u0437\u0440\u0430\u0437\u043c\u0435\u0440\u043d\u044b\u0435 (<\/strong><code><strong>int<\/strong><\/code><strong>)<\/strong><\/p>\n<p><a class=\"anchor\" name=\"3\" id=\"3\"><\/a><\/p>\n<h2>\u041c\u0430\u0441\u0441\u0438\u0432\u044b \u0438 \u0441\u0440\u0435\u0437\u044b<\/h2>\n<p>\u041e\u043a\u0435\u0439, \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u0438 \u0441\u0440\u0435\u0437\u0430\u0445. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043c\u044b \u0432\u0441\u0442\u0443\u043f\u0430\u0435\u043c \u0432 \u0437\u043e\u043d\u0443 \u043d\u0435\u0437\u0430\u0449\u0438\u0449\u0435\u043d\u043d\u043e\u0433\u043e \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u2014 \u0445\u043e\u0442\u044f Python \u0438 Go \u0432 \u0446\u0435\u043b\u043e\u043c \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b \u043f\u043e \u043f\u0430\u043c\u044f\u0442\u0438, \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 \u043d\u0435\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u043f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044e \u0431\u0443\u0444\u0435\u0440\u0430 \u0438\u043b\u0438 \u0443\u0442\u0435\u0447\u043a\u0430\u043c<a href=\"https:\/\/uproger.com\/kak-vyzyvat-funkczii-go-iz-python-s-pomoshhyu-ctypes\/\" rel=\"noopener noreferrer nofollow\">.<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/arrays\/arrays.go\" rel=\"noopener noreferrer nofollow\"><strong>arrays.go<\/strong><\/a><\/p>\n<pre><code class=\"go\">\/\/ \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u044b \u0432\u0432\u0435\u0434\u0451\u043d\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \/\/ \/\/export squares func squares(numsPtr *float64, outPtr *float64, n int64) { nums := unsafe.Slice(numsPtr, n) out := unsafe.Slice(outPtr, n)  \/\/ \u043a\u0441\u0442\u0430\u0442\u0438, \u0434\u043b\u044f Go &lt; 1.17 \/\/ nums := (*[1 &lt;&lt; 30]float64)(unsafe.Pointer(numsPtr))[:n:n] \/\/ out := (*[1 &lt;&lt; 30]float64)(unsafe.Pointer(outPtr))[:n:n]  for i, x := range nums { out[i] = x * x } } <\/code><\/pre>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/arrays\/arrays.py\" rel=\"noopener noreferrer nofollow\"><strong>arrays.py<\/strong><\/a><\/p>\n<p>\u0418 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u044d\u0442\u0443 <code>dll<\/code> \u0432 Python:<\/p>\n<pre><code class=\"python\">lib = ctypes.CDLL('.\/arrays.dll') squares = lib.squares  squares.argtypes = [     ctypes.POINTER(ctypes.c_double),     ctypes.POINTER(ctypes.c_double),     ctypes.c_int64, ]  # \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c from_buffer() \u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e, \u0447\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c: # (ctypes.c_double * 3)(*[1, 2, 3]) nums = array('d', [1, 2, 3]) nums_ptr = (ctypes.c_double * len(nums)).from_buffer(nums) out = array('d', [0, 0, 0]) out_ptr = (ctypes.c_double * len(out)).from_buffer(out)  squares(nums_ptr, out_ptr, len(nums)) print('nums:', list(nums)) print('out:', list(out)) <\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c:<\/p>\n<pre><code class=\"bash\">> python squares.py nums: [1.0, 2.0, 3.0] out: [1.0, 4.0, 9.0] <\/code><\/pre>\n<p>\u0418\u0442\u043e\u0433\u0438: \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043e \u0441\u043f\u0438\u0441\u043a\u0430\u043c\u0438 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u044b C. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>(ctypes.my_type * my_length)(1, 2, 3 ...)<\/code>. \u0411\u043e\u043b\u0435\u0435 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u2014 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 <code>array<\/code>, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0432\u044b\u0448\u0435. \u0415\u0449\u0451 \u043a\u043e\u0441\u043d\u0451\u043c\u0441\u044f \u044d\u0442\u043e\u0433\u043e \u0447\u0443\u0442\u044c \u043f\u043e\u0437\u0436\u0435, \u043a\u043e\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u043c \u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043e \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0430\u0445.<\/p>\n<p>\u0412 Go \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u0447\u0442\u043e\u0431\u044b C-\u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b \u043d\u0430 \u0441\u0440\u0435\u0437. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 Go \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0431\u0443\u0444\u0435\u0440\u0430\u043c\u0438 Python.<\/p>\n<p>\u0415\u0449\u0451 \u043f\u0430\u0440\u0430 \u043c\u043e\u043c\u0435\u043d\u0442\u043e\u0432: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c Go \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 CGo, \u044d\u0442\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u0442 \u043a \u043e\u0448\u0438\u0431\u043a\u0435. \u0412\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 C \u0438\u0437 Go \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>C.malloc()<\/code> \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e. \u041e\u0434\u043d\u0430\u043a\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u0431\u043e\u0440\u0449\u0438\u043a \u043c\u0443\u0441\u043e\u0440\u0430 \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u0442 \u0441 \u0442\u0430\u043a\u0438\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0443\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0442\u0430\u043a\u0438\u0445 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0443\u0442\u0435\u0447\u043a\u0443 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u0427\u0442\u043e \u0442\u0443\u0442 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u043e\u0432\u0430\u0442\u044c? \u0427\u0442\u043e\u0431\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043c\u043e\u0433\u043b\u0430 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u0437 Go \u043d\u0443\u0436\u043d\u043e \u043b\u0438\u0431\u043e \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0430\u043b\u043b\u043e\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043d\u0438\u0445 \u043f\u0430\u043c\u044f\u0442\u044c \u0432 Python \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0432 Go \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043b\u0438\u0431\u043e \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0432 Go \u0438 \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0432 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 (\u0447\u0443\u0442\u044c \u043a\u043e\u0441\u043d\u0451\u043c\u0441\u044f \u044d\u0442\u043e\u0433\u043e \u0434\u0430\u043b\u044c\u0448\u0435).<\/p>\n<p><strong>\u041f\u043e\u0434\u0432\u0435\u0434\u0451\u043c \u043a\u0440\u0430\u0442\u043a\u043e\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0435\u0439:<\/strong><\/p>\n<ul>\n<li>\n<p>\u0412\u043e\u0437\u0432\u0440\u0430\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439 Go \u0432 Python. <strong>\u041e\u0448\u0438\u0431\u043a\u0430.<\/strong><\/p>\n<\/li>\n<li>\n<p>\u0412\u043e\u0437\u0432\u0440\u0430\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439 C \u0438\u0437 Go \u0432 Python \u0431\u0435\u0437 \u044f\u0432\u043d\u043e\u0433\u043e \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f. <strong>\u0423\u0442\u0435\u0447\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438.<\/strong><\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0442\u0435\u0440\u044f \u0441\u0441\u044b\u043b\u043a\u0438 <code>ctypes<\/code> \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430 Go (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 <code>ctypes.addressof<\/code> \u0438 \u0441\u0431\u0440\u043e\u0441\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430-\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f). <strong>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u0430\u044f \u043e\u0448\u0438\u0431\u043a\u0430 \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438.<\/strong><\/p>\n<\/li>\n<\/ul>\n<p><a class=\"anchor\" name=\"4\" id=\"4\"><\/a><\/p>\n<h2>\u0421\u0442\u0440\u043e\u043a\u0438<\/h2>\n<p>\u0421\u0442\u0440\u043e\u043a\u0438 \u0443\u0441\u0442\u0440\u043e\u0435\u043d\u044b \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043a\u0430\u043a \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0432 \u043f\u043b\u0430\u043d\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u044c\u044e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0441\u0435, \u0447\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438, \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c\u043e \u0438 \u043a \u043d\u0438\u043c. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043e\u0431\u0441\u0443\u0434\u0438\u043c \u043f\u0430\u0440\u0443 \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0445 \u043f\u0440\u0438\u0451\u043c\u043e\u0432 \u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0434\u0432\u043e\u0434\u043d\u044b\u0435 \u043a\u0430\u043c\u043d\u0438:<\/p>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/string\/string.go\" rel=\"noopener noreferrer nofollow\"><strong>string.go<\/strong><\/a><\/p>\n<pre><code class=\"go\">\/\/export repeat func repeat(s *C.char, n int64, out *byte, outN int64) *byte { \/\/ \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u043c \u043d\u0430\u0448 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0439 \u0431\u0443\u0444\u0435\u0440 \u0432 \u0431\u0443\u0444\u0435\u0440 Go  outBytes := unsafe.Slice(out, outN)[:0] buf := bytes.NewBuffer(outBytes)  var goString string = C.GoString(s) \/\/ \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c \u0432\u0432\u043e\u0434 \u0432 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043f\u0430\u043c\u044f\u0442\u0438 Go for i := int64(0); i &lt; n; i++ { buf.WriteString(goString) } buf.WriteByte(0) \/\/ \u0432\u0430\u0436\u043d\u043e - \u043d\u0443\u043b\u0435\u0432\u043e\u0439 \u0431\u0430\u0439\u0442 \u0432 \u043a\u043e\u043d\u0435\u0446 \u0441\u0442\u0440\u043e\u043a\u0438 return out } <\/code><\/pre>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/string\/string.py\" rel=\"noopener noreferrer nofollow\"><strong>string.py<\/strong><\/a><\/p>\n<pre><code class=\"python\">lib = ctypes.CDLL('.\/string.dll') repeat = lib.repeat  repeat.argtypes = [     ctypes.c_char_p,     ctypes.c_int64,     ctypes.c_char_p,     ctypes.c_int64, ] repeat.restype = ctypes.c_char_p  #  buf_size = 1000 buf = ctypes.create_string_buffer(buf_size)  result = repeat(b'Badger', 4, buf, buf_size)  # type(result) = bytes print('Badger * 4 =', result.decode())  result = repeat(b'Snake', 5, buf, buf_size) print('Snake * 5 =', result.decode()) <\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c:<\/p>\n<pre><code class=\"bash\">> python repeat.py Badger * 4 = BadgerBadgerBadgerBadger Snake * 5 = SnakeSnakeSnakeSnakeSnake <\/code><\/pre>\n<p>\u0421\u0442\u0440\u043e\u043a\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u043f\u0443\u0442\u0435\u043c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u043a\u0438 Python \u0432 \u043e\u0431\u044a\u0435\u043a\u0442 <code>bytes<\/code> (\u043e\u0431\u044b\u0447\u043d\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0432\u044b\u0437\u043e\u0432\u0430 <code>encode()<\/code>), \u0437\u0430\u0442\u0435\u043c \u0432 C-\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0438 \u0437\u0430\u0442\u0435\u043c \u0432 Go-\u0441\u0442\u0440\u043e\u043a\u0443.<\/p>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>ctypes.c_char_p<\/code> \u0432 <code>argtypes<\/code> \u0437\u0430\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 Python \u043e\u0436\u0438\u0434\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442 <code>bytes<\/code> \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 C <code>*char<\/code>. \u0412 <code>restype<\/code> \u043e\u043d \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 <code>*char<\/code> \u0432 \u043e\u0431\u044a\u0435\u043a\u0442 <code>bytes<\/code>.<\/p>\n<p>\u0412 Go \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c <code>*char<\/code> \u0432 \u0441\u0442\u0440\u043e\u043a\u0443 Go \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>C.GoString<\/code>. \u042d\u0442\u043e \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043d\u043e\u0432\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443, \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c\u0443\u044e Go \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0441\u0431\u043e\u0440\u043a\u0438 \u043c\u0443\u0441\u043e\u0440\u0430. \u0427\u0442\u043e\u0431\u044b \u0441\u043e\u0437\u0434\u0430\u0442\u044c <code>*char<\/code> \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c <code>C.CString<\/code>. \u041e\u0434\u043d\u0430\u043a\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0442\u0435\u0440\u044f\u043d, \u0435\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u0435 \u043d\u0430 \u043d\u0435\u0433\u043e \u0441\u0441\u044b\u043b\u043a\u0443, \u0438 \u0442\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0434\u0435\u0442 \u0443\u0442\u0435\u0447\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438. \u0427\u0442\u043e\u0431\u044b \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u0437 Go, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0435 \u0436\u0435 \u043f\u0440\u0438\u0435\u043c\u044b, \u0447\u0442\u043e \u0438 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0432\u044b\u0432\u043e\u0434 \u0431\u044b\u043b \u043f\u0435\u0440\u0435\u0434\u0430\u043d Python, Go \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e, \u0438 Python \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u0441\u0442 \u0438\u0437 \u043d\u0435\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442 bytes.<\/p>\n<p><strong>\u0418\u0442\u0430\u043a, \u043a\u0430\u043a\u0438\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043c\u043e\u0433\u0443\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c?<\/strong><\/p>\n<ul>\n<li>\n<p>\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 <code>C.CString<\/code> \u0431\u0435\u0437 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u0441\u044b\u043b\u043a\u0438 \u0434\u043b\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f. <strong>\u0423\u0442\u0435\u0447\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438.<\/strong><\/p>\n<\/li>\n<li>\n<p>\u041d\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u0431\u0430\u0439\u0442\u0430 \u0432 \u043a\u043e\u043d\u0435\u0446 \u0432\u044b\u0432\u043e\u0434\u0438\u043c\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438. <strong>\u041f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0431\u0443\u0444\u0435\u0440\u0430 \u043f\u0440\u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0432 \u043e\u0431\u044a\u0435\u043a\u0442 Python.<\/strong><\/p>\n<\/li>\n<li>\n<p>\u041e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0431\u0443\u0444\u0435\u0440\u0430 \u0432 Go. <strong>\u041f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0431\u0443\u0444\u0435\u0440\u0430 \u0438\u043b\u0438 \u043d\u0435\u043f\u043e\u043b\u043d\u044b\u0439 \u0432\u044b\u0432\u043e\u0434.<\/strong><\/p>\n<\/li>\n<\/ul>\n<p><a class=\"anchor\" name=\"5\" id=\"5\"><\/a><\/p>\n<h2>\u041c\u0430\u0441\u0441\u0438\u0432 \u0441\u0442\u0440\u043e\u043a<\/h2>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u0442\u0440\u043e\u043a \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a:<\/p>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/join\/join.go\" rel=\"noopener noreferrer nofollow\"><strong>join.go<\/strong><\/a><\/p>\n<pre><code class=\"go\">func goStrings(cstrs **C.char) []string { var result []string slice := unsafe.Slice(cstrs, 1&lt;&lt;30) for i := 0; slice[i] != nil; i++ { result = append(result, C.GoString(slice[i])) } return result } <\/code><\/pre>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/join\/join.py\" rel=\"noopener noreferrer nofollow\"><strong>join.py<\/strong><\/a><\/p>\n<pre><code class=\"python\">def to_c_str_array(strs: List[str]):     ptr = (ctypes.c_char_p * (len(strs) + 1))()     ptr[:-1] = [s.encode() for s in strs]     ptr[-1] = None       return ptr <\/code><\/pre>\n<p><a class=\"anchor\" name=\"6\" id=\"6\"><\/a><\/p>\n<h2>Numpy \u0438 Pandas<\/h2>\n<p>\u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0431\u0443\u0444\u0435\u0440\u0430\u043c NumPy \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0430 <code>.ctypes.data_as(ctypes.whatever)<\/code>. \u0412 pandas \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0442\u0440\u0438\u0431\u0443\u0442 <code>.values<\/code> \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0431\u0430\u0437\u043e\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 NumPy, \u0430 \u0437\u0430\u0442\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 NumPy \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043c\u043e\u0436\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\/\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043d\u0430 \u043c\u0435\u0441\u0442\u0435, \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0430\u043f\u043e\u0434\u043e\u0431\u0438\u0435:<\/p>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/numpypandas\/numpypandas.go\" rel=\"noopener noreferrer nofollow\"><strong>numpypandas.go<\/strong><\/a><\/p>\n<pre><code class=\"go\">\/\/export increase func increase(numsPtr *int64, n int64, a int64) { nums := unsafe.Slice(numsPtr, n) for i := range nums { nums[i] += a } } <\/code><\/pre>\n<p><a href=\"https:\/\/github.com\/fluhus\/snopher\/blob\/master\/src\/numpypandas\/numpypandas.py\" rel=\"noopener noreferrer nofollow\"><strong>numpypandas.py<\/strong><\/a><\/p>\n<pre><code class=\"python\">lib = ctypes.CDLL('.\/numpypandas.dll') increase = lib.increase  increase.argtypes = [     ctypes.POINTER(ctypes.c_int64),     ctypes.c_int64,     ctypes.c_int64, ]  people = pandas.DataFrame({     'name': ['Alice', 'Bob', 'Charlie'],     'age': [20, 30, 40], })  # \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0442\u0438\u043f ages = people.age if str(ages.dtypes) != 'int64':     raise TypeError(f'Expected type int64, got {ages.dtypes}')  values = ages.values  # type=numpy.Array ptr = values.ctypes.data_as(ctypes.POINTER(ctypes.c_int64))  print('Before') print(people)  print('After') increase(ptr, len(people), 5) print(people) <\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c:<\/p>\n<pre><code class=\"bash\">> python table.py Before       name  age 0    Alice   20 1      Bob   30 2  Charlie   40 After       name  age 0    Alice   25 1      Bob   35 2  Charlie   45 > <\/code><\/pre>\n<p>\u0412\u0430\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-378711","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/378711","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=378711"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/378711\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=378711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=378711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=378711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}