4 ms·
I wonder whether I can use it to create a Go library that I can import in Python?
by pknerd 4y ago
I wonder whether I can use it to create a Go library that I can import in Python?
- linux2647 4y agoMy read of the library is it only works the other way: you could import a Python library from Go
- Yoric 4y agoThat sounds a little scary. I have no idea how the Go gc would interact with Python. There might be a way, too.
- pknerd 4y agoCGo converts Go code to a C based .so library which you can call in Python.
- Yoric 4y agoWhat'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?
- slimsag 4y agoGP 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 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.
- Yoric 4y agoThanks!
- throwaway894345 4y agoYou 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").
- pknerd 4y agoI 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
- totallygamerjet 4y agoI’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.
- pjmlp 4y agoYou can already do this today with standard toolchain. https://medium.com/analytics-vidhya/running-go-code-from-python-a65b3ae34a2d https://medium.com/analytics-vidhya/running-go-code-from-pyt...