totallygamerjet
totallygamerjet on Hacker News
Re:Purego – A library for calling C functions from Go without Cgo
on Sunday February 12, 2023 @03:29PM (#34766559)
I don't have either. I was gonna figure out how to post it after I actually sat down and wrote it lol. I'll probably post it in the golang subreddit and maybe link to it in the README.md since it describes how purego works.
Re:Purego – A library for calling C functions from Go without Cgo
on Sunday February 12, 2023 @02:22PM (#34765916)
"In Go object files and binaries, the full name of a symbol is the package path followed by a period and the symbol name: fmt.Printf or math/rand.Int. Because the assembler's parser treats period and slash as punctuation, those strings cannot be used directly as identifier names. Instead, the assembler allows the middle dot character U+00B7 and the division slash U+2215 in identifiers and rewrites them to plain period and slash."[0]
[0] - https://go.dev/doc/asm
Re:Purego – A library for calling C functions from Go without Cgo
on Sunday February 12, 2023 @02:11PM (#34765784)
What slimsag wrote is correct. It makes cross-compiling code that needs to call C functions as easy a setting the GOOS and GOARCH and just building. This means no need to worry about building a C cross-compiler.
I do want to write an article about how purego works under the hood.
Re:Purego – A library for calling C functions from Go without Cgo
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 without Cgo
on Sunday February 12, 2023 @12:16PM (#34764611)
Yeah the default behavior is to switch back. It’s possible to pin a goroutine to a thread with runtime.LockOSThread(). However I don’t believe it avoids the stack switching. It’s purpose it to make sure that Thread Local Storage works properly. The runtime is pretty smart though so it might already do the optimization you suggested in someway. I know it has a goroutine specifically for monitoring if a thread is stuck in a external call and therefore spawns a new thread to continue work (sysmon)
Re:Purego – A library for calling C functions from Go without Cgo
on Sunday February 12, 2023 @12:09PM (#34764526)
It’s pretty simple to use if you are familiar with dlopen and friends.
Just call purego.Dlopen(“libname.so”, purego.RTLD_GLOBAL)
Take the returned library (make sure to check for errors with purego.Dlerror() first) and call purego.Dlsym(lib, “cfuncName”). If it exists than u can call it with either purego.SyscallN or purego.RegisterFunc
Re:Purego – A library for calling C functions from Go without Cgo
on Sunday February 12, 2023 @12:02PM (#34764450)
Contributor here: Purego doesn’t do anything to improve the overhead of calling into C. It uses the same mechanisms that Cgo does to switch to the system stack and then call the C code. Purego just avoids having to need a C toolchain to cross compile code that calls into C from Go.
I’ve actually been quite interested in Zig. If that built-in was added than it would likely be possible to grow the goroutine stack to the proper size and than call the Zig code. Very interesting stuff!