I'm building an app and the backend preferably would interface with Rust. I could just do that with Rails and FFI but I would much rather the benefit of a statically typed language. Is there a way to do this? I can't seem to find any good documentation online.
Thank you!
Edit: I believe I found what I was looking for. This project is a little tutorial for building rust libraries and running them with Go. But I'll leave this up here in case anyone needs it in the future.
Go is great.
评论:
epiris:
ethyn_bunt:You could use cgo if you want to call the function from your Go application. If the rust system is large enough maybe considering separating it into its own service and creating an official contract between the two using grpc. Typically I think it's good to stay away from cgo unless you end up hitting a road block that leaves no other options.
epiris:Thanks for the advice! Are there any real downsides to cgo?
connor4312:Yes I think working with cgo is just nuanced and unpleasant in general, but I also don't like working with c or c++ anymore so your mileage may vary. A more concrete reason to avoid it anything beyond hello world with basic types you end up importing unsafe to work outside the type system.
- An overhead to calls across the cgo<-> go boundary, a few hundred
msns - Cgo calls establish full C threads and are significantly more expensive than lightweight Go calls
- It makes compiling and consuming your application much more difficult
Dave Cheney goes into much more detail in this great article: https://dave.cheney.net/2016/01/18/cgo-is-not-go
dlsniper:connor4312:An overhead to calls across the cgo<-> go boundary, a few hundred ms
False, it is massively improved since 1.8 and never was hundreds of ms of begin with.
Cgo calls establish full C threads and are significantly more expensive than lightweight Go calls
Depending on your needs, this might not be a problem at all.
It makes compiling and consuming your application much more difficult
False, the compilation remains the same and if you layout the packages in a nice way, you pay the C compilation penalty only when you actually have changes in the C files, much like the Go compiler does for the .go files.
dlsniper:Whoa, sorry, didn't mean to personally offend or anything! Happy to answer questions and discuss to clarify what I said.
False, it is massively improved since 1.8 and never was hundreds of ms of begin with.
I made a super trivial cgo program here and benchmarked it on 1.8.1
➜ cgo_test go test -bench=. ./ BenchmarkCgoCall 20000000 96.4 ns/op PASS ok _/home/connor/cgo_test 2.030s
The hundreds might have been a bit high, but bear in mind that for any non-trivial function you're probably going to be passing in data that needs to be allocated and copied over to the C heap. Even if it's just a filename for C to do further processing on. "Real world" overheads are usually in the hundreds of nanoseconds. Just passing in a string to the function, for instance, bumps it up to 372 ns/op.
edit: realize I wrote ms instead of ns in my comment above, I meant ns. My bad!
Depending on your needs, this might not be a problem at all.
Totally agree!
False, the compilation remains the same and if you layout the packages in a nice way
Hm, that's interesting. For me, compilation of that sample program I linked above failed on Windows with Cygwin -- not a particularly exotic environment.
{ cgo-test } » go test -bench=. ./ gcc: error: CreateProcess: No such file or directory FAIL _/C_/Users/Connor/Downloads/cgo-test [build failed]
Ended up running it on a Linux VM. Indeed, it looks like cgo in cygwin has no shortage of issues and, at least four years ago, no support. Considering I develop normal Go code perfectly fine in cygwin, I would say this is making compilation more difficult.
definitelynotpietro:Whoa, sorry, didn't mean to personally offend or anything!
How did you even got that?
dlsniper:probably your aggressive use of the word FALSE
definitelynotpietro:How do you say: this thing is false without using false? If I use "incorrect" instead would that make any difference? The statement is still false and I still have no control on how you choose to read the sentence. And for the record, I used "False", not "FALSE".
dlsniper:Dunno. When I see the world false, I read it in Dwight's voice and it bothers me. Reading "incorrect" seems pretty low-impact. Seems like I'm not alone
also, only angry nerds actually use the word false in conversation, so there's that.
edit: you can also totally correct someone without prefacing the correction with the words false or incorrect. they're implied by the correction, no? wtf am I even having this conversation just stop being an angry nerd
wtf am I even having this conversation just stop being an angry nerd
I'm very relaxed, it seems you are the one thinking I'm not (for whatever reason). Maybe try not to assume this much about plain text next time? It really is a terrible medium to convey emotions :)
