1. 38
  1.  

  2. 8

    It is pretty damming to the Go language that you can’t use any existing code. Just about every other language provides relatively straightforward (if not seamless) interop with any other C ABI. Only in Go have I heard such consistent and negative opinions on the FFI. Java and JNI are close, but it still seems better received and in that case at least there is a decent reason because ruins your portability once you add native code.

    The fact that someone would recomend “reimplementing a large piece of C code in Go” instead of just binding to it is exposing a huge downside of the language.

    1. 4

      The fact that someone would recomend “reimplementing a large piece of C code in Go” instead of just binding to it is exposing a huge downside of the language.

      The main reason is so you “get” effortless portability as a result. I can only think of zig where you get some out-of-the-box portability without re-writing your C in zig (since it ships with libc for various platforms/archs and has all the toolchain nicely setup).

      1. 2

        i immediately thought of zig’s self contained compiler when i saw this post… and i recall things being posted to show how you can integrate zig cc in w/ go/cgo to have portable c compilers

        seems like it would be a good thing for these project maintainers to get on board with…

        1. 6

          I wrote a blog post where I cross-compiled with Zig the CGo SQLite library for all the major OSs without too much fuss.

          https://zig.news/kristoff/building-sqlite-with-cgo-for-every-os-4cic

      2. 3

        I can’t wait for Go to have an FFI some day!

        1. 1

          As mentioned above, I believe this to be simply untrue: Go has an FFI today and it’s called cgo. What is it about cgo that does not make it an FFI?

          1. 1

            cgo is basically a separate language. It is a separate implementation.

            1. 1

              I can’t see how it’s a separate language. You embed a bit of C code in a special place within a Go file. The C code is compiled by a C compiler, the Go code by a compiler and cgo. And from the C and the Go code, cgo generates some interface code to make C names known to the Go compiler and some Go names known to the C compiler. How is cgo (which, to me, is a program) a separate language?

              It is a separate implementation.

              cgo is a separate implementation of what?

        2. 2

          Yes 100%, here is my lament from 4 years ago on that topic.

          https://news.ycombinator.com/item?id=16741043

          A big part of my pain, and the pain I’ve observed in 15 years of industry, is programming language silos. Too much time is spent on “How do I do X in language Y?” rather than just “How do I do X?”

          For example, people want a web socket server, or a syntax highlighting library, in pure Python, or Go, or JavaScript, etc. It’s repetitive and drastically increases the amount of code that has to be maintained, and reduces the overall quality of each solution (e.g. think e-mail parsers, video codecs, spam filters, information retrieval libraries, etc.).

          There’s this tendency of languages to want to be the be-all end-all, i.e. to pretend that they are at the center of the universe. Instead, they should focus on interoperating with other languages (as in the Unix philosophy).

          One reason I left Google over 6 years ago was the constant code churn without user visible progress. Somebody wrote a Google+ rant about how Python services should be rewritten in Go so that IDEs would work better. I posted something like <troll> … Meanwhile other companies are shipping features that users care about </troll>. Google+ itself is probably another example of that inward looking, out of touch view. (which was of course not universal at Google, but definitely there)

          This is one reason I’m working on https://www.oilshell.org – with a focus on INTEROPERABILITY and stable “narrow waists” (as discussed on the blog https://www.oilshell.org/blog/2022/02/diagrams.html )

          (copy of HN comment in response to almost the same observation!)

          I’m also excited about Zig for this reason. e.g. “maintain it with Zig” https://kristoff.it/blog/maintain-it-with-zig/

          1. 1

            On the other hand, oilshell is not(?) compatible with the piles of bash (and sh, and…) scripts out in the world, so folks have to rewrite it to be compatible with your shell. Is this not contradicting what you said earlier?

            1. 2

              Hm honest question: Why do you think it’s not compatible?

              It’s actually the opposite – it’s the ONLY alternative shell that’s compatible with POSIX sh and bash. It’s the most bash compatible shell by a mile.

              Try running osh myscript.sh on your shell scripts and tell me what happens!


              The slogan on the front page is supposed to emphasize that, but maybe it’s not crystal clear:

              It’s our upgrade path from bash to a better language and runtime.

              Also pretty high up on the FAQ is the statement:

              http://www.oilshell.org/blog/2021/01/why-a-new-shell.html#introduction

              OSH is a shell implementation that’s part of the Oil project. It’s compatible with both POSIX and bash. The goal is to run existing shell scripts. It’s done so since January 2018, and has matured in many regular releases since then.


              Nonetheless I think it could be clearer, so I filed a bug to write a project tour and put it prominently on the home page:

              https://github.com/oilshell/oil/issues/1127

              It is disappointing to me that this hasn’t been communicated after so many years … I suspect that some people actually think the project is impossible. It’s both compatible AND it’s a new language.

              I described in the latest blog post how that works:

              http://www.oilshell.org/blog/2022/05/release-0.10.0.html#backward-compatibility-the-eternal-puzzle

              Here are all your other alternative shell choices, NONE of which have the compatibility of OSH.

              https://github.com/oilshell/oil/wiki/Alternative-Shells

              (That is why the project is so large and long)

              1. 1

                Ah, I’m sorry. I skimmed the FAQ but missed that sentence. For some reason, the impression I got from your FAQ is that it’s basically yet another shell that doesn’t offer backwards compatibility. Obviously I was terribly wrong. I’m not sure how to suggest changes that may have prevented that (other than it’s completely my fault for misreading/skimming and getting the wrong impression.) So, sorry for the noise.

                1.  

                  OK no worries … I think it actually did point out that this crucial fact about the project is somewhat buried. Not entirely buried but “somewhat”.

          2. 1

            Yes and no. I mean there is CGO. Which you can use. While it’s worse in Go, also because of threading, especially on Linux you’ll still find “pure” implementations of things that would usually use a C library, sometimes they are faster because calling the FFI might still be slow. Database interfaces are one such an example, where people sometimes find the bottleneck to be the FFI.

            You also get certain benefits from not using C. I already mentioned the threading part which sometimes bites people in Go, but also you can be sure about memory safety, debugging will be easier, all the people using the project can contribute even when they are not fluent in C, etc.

            And if you still want/need to use C, there is CGO.

            There certainly have been cases in other languages where I wished a library wasn’t just a wrapper around C, be it Python, Java, Objective-C/Swift or in node.js-projects. Given circumstances they can be a source for headaches.

            1. 1

              It is simply not true that “you can’t use any existing code” in Go. There’s cgo and it allows you to to call into C code and provides way for C code to call into go code - that’s pretty much the definition of using existing code. I think a big reason people are complaining about JNI is the the same for people complaining about cgo: Because you are dealing with garbage collected language, there are rules about what you can do with memory and pointers. The same applies to .NET as well.

              The fact that someone would recomend “reimplementing a large piece of C code in Go” instead of just binding to it is exposing a huge downside of the language.

              As the article points out, in the very first sentence, most people use mattn/go-sqlite3 which is in fact a wrapper around the canonical C SQLite implementation. A “decent reason” (your words) to not use that library is because “it ruins your portability” because “you add native code”. This reason is at play here.

              This being said, the shown port to Go is a respectable effort. While being impressive, I’d probably use one of the bindings to the canonical C code if possible as it uses a highly tested implementation. If not possible the cznic provides an interesting alternative.

            2. 4

              I’m reluctant to use anything apart from the original, since I cannot predict in what hot waters I’m getting myself into in the future. And I’m saying that as a Go beginner that failed to get a static build with SQLite and the ICU extension. After a 14-hour struggle, I gave up and switched the deployment to a dynamic executable.

              1. 3

                Thanks for this! Great post.

                1. 3

                  One errata: an HN commenter pointed out that the mattn SELECTs weren’t completing. With that fixed INSERTs are still twice as slow but SELECTs are at worst twice as slow and at best 10% as slow.

                  1. 2

                    I hope we will see a follow up post in 2-3 weeks with a nice speedup in modernc.org/sqlite.

                    1. 1

                      Is there an upcoming improvement?

                      1. 3

                        “I hope”

                    2. 1

                      I wonder what would be involved to speed things up a bit. There might be a few easy wins.