Re:Purego – A library for calling C functions from Go withou...
by pknerd on Sunday February 12, 2023 @11:39AM
(#34764202)
I wonder whether I can use it to create a Go library that I can import in Python?
[ Parent
| on HN ]
Re:Purego – A library for calling C functions from Go withou...
by linux2647 on Sunday February 12, 2023 @11:40AM
(#34764217)
My read of the library is it only works the other way: you could import a Python library from Go
Re:Purego – A library for calling C functions from Go withou...
by Yoric on Sunday February 12, 2023 @12:04PM
(#34764472)
That sounds a little scary. I have no idea how the Go gc would interact with Python.
There might be a way, too.
Re:Purego – A library for calling C functions from Go withou...
by pknerd on Sunday February 12, 2023 @12:12PM
(#34764565)
CGo converts Go code to a C based .so library which you can call in Python.
Re:Purego – A library for calling C functions from Go withou...
by Yoric on Sunday February 12, 2023 @04:21PM
(#34767133)
What's the garbage-collection story? One gc per .so? Or is there some mechanism to share a gc instance between several .so compiled by go?
Re:Purego – A library for calling C functions from Go withou...
by slimsag on Sunday February 12, 2023 @05:57PM
(#34768053)
GP is not exactly right; CGO is not involved in building a shared library except for exporting C functions which can be called. It doesn't "convert Go code to C code" or anything like that.
`-buildmode=c-shared` is what produces a shared library with a C ABI exposed; https://golang.org/s/execmodes was the design document for it, which explains:
> It follows that all Go code shares a single runtime. All Go code uses the same memory allocator, the same goroutine scheduler, and in general acts as though it were linked into a single Go program. This is true even when multiple shared libraries are involved.
Re:Purego – A library for calling C functions from Go withou...
by Yoric on Monday February 13, 2023 @12:43PM
(#34777104)
Thanks!
Re:Purego – A library for calling C functions from Go withou...
by throwaway894345 on Sunday February 12, 2023 @12:10PM
(#34764534)
You can already do this with CGo. The difference between CGo and purego is that CGo requires you have a C toolchain installed while purego seems to allow you to call a function that has already been compiled apart from the Go build system. In either case, you can call the Go library from Python, but the tough bits are translating Go objects into Python objects (and vice versa) as well as making sure object lifetimes are correctly managed (I think this is mostly "copy data rather than passing pointers across the language boundary").
Re:Purego – A library for calling C functions from Go withou...
by pknerd on Sunday February 12, 2023 @12:13PM
(#34764580)
I know about CGo and have used it. The post title, "A library for calling C functions from Go without Cgo", made me curious how do I achieve something similar with PureGo without using CGO
Re:Purego – A library for calling C functions from Go withou...
by totallygamerjet on Sunday February 12, 2023 @12:26PM
(#34764709)
I’m one of the main contributors. I’ve looked into it bc I wanted to know if I could build iOS apps without Cgo. ATM, it is not possible. The reason is because when you run go build creating a shared object it runs the go cgo tool. That tool although written entirely in Go doesn’t know about purego and so will go ahead and import runtime/cgo which requires a C toolchain. Now it could be possible to circumvent that with using a custom Go build toolchain but the goal of purego was to be seemless to use in a project. Just use it and then go build like any other dependency.
Re:Purego – A library for calling C functions from Go withou...
by pjmlp on Monday February 13, 2023 @09:57AM
(#34774164)
You can already do this today with standard toolchain.
https://medium.com/analytics-vidhya/running-go-code-from-pyt...