News for hackers. Stuff that matters.
Re:Generics can make your Go code slower by svnpenn on Wednesday March 30, 2022 @01:49PM (#30858480)
Typical gatekeeping. I like Go, because it lets me get stuff done. You could say the same about JavaScript, but I think Go is better because of the type system. C, C++ and Rust are faster in many cases, but man are they awful to work with.

C and C++ dont really have package management to speak of, its basically "figure it out yourself". I tried Rust a couple of times, but the Result/Option paradigm basically forces you into this deeply nested code style that I hate.

Re:Generics can make your Go code slower by pornel on Wednesday March 30, 2022 @02:03PM (#30858641)
Nobody's saying you can't use Go or must use C/C++/Rust. If Go works for you, that's great.

The issue is about positioning of Go as a language. It's confusing due to being (formerly) marketed as a "systems programming language" that is typically a domain of C/C++/Rust, but technically Go fits closer to capabilities of Java or TypeScript.

Re:Generics can make your Go code slower by pjmlp on Wednesday March 30, 2022 @02:34PM (#30859039)
Is writing a compiler, linker, kernel emulation layer, TCP/IP stack or a GPU debugger, systems programming?
Re:Generics can make your Go code slower by pornel on Wednesday March 30, 2022 @08:19PM (#30862421)
If you dig deep enough into the Turing Tarpit, you can write these in JavaScript too (in fact, some of these have already been written).

It's merely a disagreement about what the label "systems programming" should mean, rather than about capabilities and performance of the Go language. Objectively and undeniably Go relies on garbage collection, and C/C++/Rust don't. Google's implementation of Go prefers fast compilation speed over maximizing run-time efficiency, and C/C++/Rust implementations aim for zero-(runtime)cost abstractions.

I hope it's clear there's no argument about the substantial technical differences between these languages. They have clearly different trade-offs, regardless of what name you think is appropriate to use for that difference.

Re:Generics can make your Go code slower by pjmlp on Thursday March 31, 2022 @01:28AM (#30864101)
Re:Generics can make your Go code slower by pornel on Thursday March 31, 2022 @06:21AM (#30865595)
I'm saying forks aren't for eating soup, and you're showing me soups eaten with a fork.

In case of languages' general capabilities/applicability, I don't think singular counterexamples are sufficient, because they prove "can", rather than "should". Turing Tarpit means you can push languages way beyond their appropriate use-cases.

There are also weird cases like Java smart cards and LISP machines, but I don't think it's useful to use these exceptions to classify Java and LISP as "assembly" or "machine" languages. Go does very well in the network services niche, but you can also write such network services in Bash (determined-enough people have written Bash HTTP servers). You can write small shell utilities in Go too. Does this mean Go and Bash are the same type of language?

The lines are blurry, but you have to draw a line somewhere, otherwise every language is a shell scripting systems programming assembly machine language.

Re:Generics can make your Go code slower by averagedev on Wednesday March 30, 2022 @01:57PM (#30858583)
I've found Go to be much simpler than Rust, especially syntax wise. However, in Rust you can use the ? operator which propagates errors. In Go you have to check err != nil.
Re:Generics can make your Go code slower by svnpenn on Wednesday March 30, 2022 @02:32PM (#30859022)
> Rust you can use the ? operator

That doesnt work with all types:

https://stackoverflow.com/a/65085003

Re:Generics can make your Go code slower by jdmnd on Wednesday March 30, 2022 @03:24PM (#30859714)
It works for any type that implements the `std::error::Error` trait; which is something you can easily implement for your own types. If you want your errors to be integers for some reason, you can wrap that type in a zero-sized "newtype" wrapper, and implement `Error` for that.

The Stack Overflow answer you linked seems to be claiming that it's simply easier to return strings, but I wouldn't say this is a restriction imposed by the language.

Re:Generics can make your Go code slower by svnpenn on Wednesday March 30, 2022 @04:13PM (#30860213)
> easily implement for your own types

have you ever actually done that? I have, its not easy. Please dont try to hand wave away negatives of the Rust type system.

Re:Generics can make your Go code slower by TheDong on Wednesday March 30, 2022 @04:35PM (#30860461)
> have you ever actually done that? I have, its not easy.

Yes. I do it frequently. "#[derive(Error, Debug)]": https://github.com/dtolnay/thiserror#example

Much easier than implementing the error interface in go.

Rust is powerful enough to allow macros to remove annoying boiler-plate, and so most people using rust will grab one of the error-handling crates that are de-facto standard and remove the minor pain you're talking about.

In go, it's not really possible to do this because the language doesn't provide such macros (i.e. the old third-party github.com/pkg/errors wanted you to implement 'Cause', but couldn't provide sugar like 'this-error' does for it because go is simply less powerful).

I've found implementing errors in go to be much more error-prone and painful than in rust, and that's not to mention every function returning untyped errors, meaning I have no clue what callers should check for and handle new errors I add.

Re:Generics can make your Go code slower by svnpenn on Wednesday March 30, 2022 @05:08PM (#30860800)
> Much easier than implementing the error interface in go.

is this a joke? You have to import a third party package, just to implement an error interface? Here is Go example, no imports:

    type errorString string

    func (e errorString) Error() string {
       return string(e)
    }
Re:Generics can make your Go code slower by TheDong on Wednesday March 30, 2022 @05:45PM (#30861200)
It was not a joke.

Let's look at a common example: you want to return two different types of errors and have the caller distinguish between them. Let me show it to you in rust and go.

Rust:

    #[derive(Error, Debug)]
    pub enum MyErrors {
       #[error("NotFound: {0}")
       NotFound(String),
       #[error("Internal error")]
       Internal(#[source] anyhow::Error),
    }
The equivalent go would be something like:

    type NotFoundErr struct {
        msg string
    }

    func (err NotFoundErr) Error() string {
        return "NotFound: " + err.msg
    }

    func (err NotFoundErr) Is(target error) bool {
        if target == nil {
            return false
        }
        // All NotFoundErrs are considered the same, regardless of msg
        _, ok := target.(NotFoundErr)
        return ok
    }

    type InternalErr struct {
        wrapped error
    }
    
    func (err InternalErr) Error() string {
        return fmt.Sprintf("Internal error: %s", err.wrapped)
    }

    func (err InternalErr) Unwrap() error {
        return err.wrapped
    }
Re:Generics can make your Go code slower by svnpenn on Wednesday March 30, 2022 @06:46PM (#30861789)
I dont think you realize how ridiculous this comment is. Youre comparing 10 lines of Go, with 200 of Rust:

https://github.com/dtolnay/thiserror/blob/master/src/lib.rs

Re:Generics can make your Go code slower by TheDong on Wednesday March 30, 2022 @07:07PM (#30861969)
What's the difference between importing some hundred's of lines from thiserror in rust vs importing the "error" package in go?

If the difference is just "It's okay to use stdlib code, but not dependencies", then go's a great language by that metric.

I don't think that's what matters at all. What matters is the abstraction that the programmer deals with.

In go, the abstraction I deal with, as a programmer, is what I showed above.

In rust, the abstraction I deal with is also what I showed above. One of those things is simpler.

Further, the abstraction in go is leakier. My function returns a 'error' interface even if it can only be those two concrete types, so the caller of my function has to step into my function, reason through it to figure out all the error variants, and then check them with 'errors.Is' or such, and changes to what variants are returned aren't statically checked.

In rust, I can look at the type-signature, write a 'match' statement, and never have to manually figure out all the variants returned, since the compiler just knows.

My point here is that what matters is the abstraction that programmers interact with. Third party libraries are bad when they give a leaky abstraction that causes the programmer pain. Standard libraries likewise.

In this case, the abstraction available in rust is cleaner and requires less code for me, the user of the package, so I don't think the lines of code used to build that abstraction matter.

Why do you see this as something that matters?

Re:Generics can make your Go code slower by svnpenn on Wednesday March 30, 2022 @07:16PM (#30862045)
> What's the difference between importing some hundred's of lines from thiserror in rust vs importing the "error" package in go?

Again, you're comparing apples and oranges. It seems you didn't see my previous example, here it is again:

    type errorString string

    func (e errorString) Error() string {
       return string(e)
    }
Re:Generics can make your Go code slower by TheDong on Wednesday March 30, 2022 @07:25PM (#30862099)
I addressed that with the start of my comment: "Let's look at a common example: you want to return two different types of errors and have the caller distinguish between them"

Yes, your example implements the error interface, but it's not realistic. There is real go code that does that, but that's the reason I have to do string-matching type error handling in go, and frankly it's an argument against the language that its error handling is such a mess that some people see that as reasonable.

Having code that does

    return errorString("You have to do string matching on me internal error")
is something you can do, sure, but it's not good code. Idiomatically in go, it would also be "errors.New", not this errorString type you made.

In rust, that would also be just as easy since it would be:

    bail!("oh no internal error")
using anyhow, which is the de-facto standard way to do crappy string-style erroring, like the above.

But that's not what I want to talk about since that sort of error handling isn't interesting in either language, and isn't where they differ.

Re:Generics can make your Go code slower by svnpenn on Wednesday March 30, 2022 @07:34PM (#30862140)
You never gave a Rust example without using the 200 lines of Rust package.

You didnt do so, because implementing an error interface in Rust is a painful and extremely verbose process. Its not in Go, as I demonstrated.

> bail!("oh no internal error")

another Rust package. Are you unable to just write Rust without importing something?

Re:Generics can make your Go code slower by TheDong on Wednesday March 30, 2022 @07:37PM (#30862162)
As I said above:

> In this case, the abstraction available in rust is cleaner and requires less code for me, the user of the package, so I don't think the lines of code used to build that abstraction matter.

> Why do you see this as something that matters?

It's true that the abstraction in rust has more code underlying it, but why does that matter?

If you de-facto never have to implement the rust error trait by hand due to excellent library support, then it's a moot point, isn't it?

Anyway, my examples above did implement the error trait, simply by deriving it.

> Are you unable to just write Rust without importing something?

Rust does have less in the stdlib, yes. If you want a json serializer in go, that's the stdlib. In rust, it's the serde package.

Rust is intentionally a smaller standard library. I don't think there's anything wrong with that, and I personally prefer it since it allows for more innovation on things like http / json / error interfaces / etc.

I don't know why you're phrasing it like it's a bad thing.

Re:Generics can make your Go code slower by svnpenn on Wednesday March 30, 2022 @07:40PM (#30862178)
Thats a good outlook. Dont worry about any problem, just import something an its fixed! More imports the better! Hopefully we can do away with all code, and just import everything.
Re:Generics can make your Go code slower by unscaled on Wednesday March 30, 2022 @11:47PM (#30863634)
Well, before Go 1.13 came out, every decent production codebase in Go had to import Dave Cheney's pkg/errors[1] unless you totally gave up the idea of having useful error traces.

Go 1.13 has incorporated most of what this package does into the standard library. It really doesn't matter much whether the code is in a de-facto standard package or folded into the standard library IMHO, but Rust could do the same.

[1]: https://github.com/pkg/errors

Re:Generics can make your Go code slower by steveklabnik on Thursday March 31, 2022 @01:21AM (#30864065)
Rust did partially do this; one of the previous generation of packages here, failure, had most of its ideas eventually rolled into std::errror::Error, which enabled this next generation of packages (anyhow, thiserror, miette, eyre... the only one I'm not sure about is snafu, which did exist before, but I'm not sure if it got more improvements due to these changes or not.)
Re:Generics can make your Go code slower by TheDong on Wednesday March 30, 2022 @08:32PM (#30862520)
> Dont worry about any problem, just import something an its fixed! More imports the better!

We're talking about error boilerplate here.

Just as I do not write assembly by hand, but instead let the compiler generate that, just as I don't write http clients by hand, but let someone else do that, yes I would also rather not write error boilerplate by hand.

I would love it if I didn't have to write as much code to solve problems, but I recognize that the problems I'm solving are mostly specific enough that they require bespoke code specialized to the problem space.

However, error handling? Yeah, I don't care to hand-write that.

Said less glibly, I will happily import stuff when it makes my life easier, and write it myself when that is better.

Re:Generics can make your Go code slower by bschwindHN on Thursday March 31, 2022 @09:58PM (#30874417)
Don't you think that's a little disingenuous?

You're absolutely right about implementing the `Error` trait yourself in Rust, it's a pain! And with many things, the Rust team felt it was better to leave error experimentation to an external library, as the community can iterate more quickly and settle on a favorite. At the moment, that favorite is `thiserror` (for defining errors in a library) and `anyhow` (for working with errors in application code)

That's been a pretty common pattern in Rust - leave some less clear parts of the standard library out for external crates to implement and experiment on. So you'll see almost all big Rust projects use one of those error handling libraries. You _could_ implement it with just the standard library, but you're just giving yourself more work for little to no gain.

Here's an example without using external libraries:

https://play.rust-lang.org/?version=stable&mode=debug&editio...

Not particularly good or bad (and I didn't even replicate all the functionality that `thiserror` gives you).

So yeah, Go and Rust have different philosophies for their standard library, and you can argue about which is better, but I don't think one can be proved objectively better than the other.

Re:Generics can make your Go code slower by jbjohns on Thursday March 31, 2022 @09:53AM (#30867180)
The Rust code is doing much more than what the Go code is doing, it's making more incorrect cases impossible. I really don't understand the complaint about imports. Anything that's not built into the standard library is not valid somehow? I'm struggling to imagine what would make someone think this.
Re:Generics can make your Go code slower by unscaled on Wednesday March 30, 2022 @11:42PM (#30863608)
You've did notice the file you've linked to only has 7 non-comment/documentation lines, right?

Now, to be fair, there are 50 or so lines of code in other files, but I would still love to see a useful Go package which is that small.

Re:Generics can make your Go code slower by mcronce on Wednesday March 30, 2022 @04:33PM (#30860435)
I do it frequently. It is indeed easy
Re:Generics can make your Go code slower by monocasa on Wednesday March 30, 2022 @04:36PM (#30860474)
Only automatically printing something when returning it from main doesn't work with all types with the ? operator. And frankly 'handling errors by auto print and exit' is a bit of a code smell anyway, it's not much better than just .unwrap() on everything in main.
Re:Generics can make your Go code slower by mcronce on Wednesday March 30, 2022 @02:11PM (#30858745)
What, specifically, do you mean when you say Rust is "awful to work with"? With C and C++ I agree, but I've had a drastically better development experience in Rust than Go.
Re:Generics can make your Go code slower by svnpenn on Wednesday March 30, 2022 @02:34PM (#30859046)
You should probably read the rest of the comment...
Re:Generics can make your Go code slower by mcronce on Wednesday March 30, 2022 @03:21PM (#30859687)
Are Result and Option really the only thing? Because nesting scopes based on Err/None is rarely the right choice, just like nesting a scope based on `if err == nil` isn't typically something you want to do in Go, or `if errno == 0` in C

  - You can panic trivially with `.unwrap()`
  - You can propagate the condition up trivially with `?`
    - `?` doesn't work with all types, but it does work with Option, and it does work with the vast majority of error types - making your custom error work with it is very easy (if you're whipping up an application or prototype and want your error handling very simple, `anyhow::Error` works great here)
  - You can convert None to an Err condition trivially with `.ok_or()?`
  - In cases where it makes sense, you can trivially use a default value with `.unwrap_or_default()`
And all of these use require a _lot_ less code than `if err != nil { return nil, err }`

And all of these allow you to use the Ok/Some value directly in a function call, or in a method chain, while still enabling the compiler to force you to handle the Err/None case

The common theme here being "trivial" :) Result/Option are a big piece of that better developer experience.

Re:Generics can make your Go code slower by rastignack on Thursday March 31, 2022 @03:22AM (#30864702)
However in typical rust code bases this systematic wrapping causes a lot of unhandled panics.

Reading it feels just wrong.

Re:Generics can make your Go code slower by ohYi55 on Wednesday March 30, 2022 @02:07PM (#30858703)
git clone?

I mean do we need bespoke package management tooling for everything now?

Seems like an outdated systems admin meme that violates KISS, explodes dependency chains, risks security, etc. IT feels infected by sunk cost fallacy.

It’s electron state in machines. The less altogether the better.

Re:Generics can make your Go code slower by dahfizz on Wednesday March 30, 2022 @01:57PM (#30858577)
> C and C++ dont really have package management to speak of

I hear this complaint often, but I consider it a feature of C. You end up with much less third party dependencies, and the libraries you do end up using have been battle tested for decades. I much prefer that to having to install hundreds of packages just to check if a number is even, like in JS.

Re:Generics can make your Go code slower by throwaway894345 on Wednesday March 30, 2022 @02:54PM (#30859338)
I think you're contradicting yourself. You end up with fewer third party dependencies in C because C developers end up rewriting from scratch what they would otherwise import, and these rewrites have much less battle-testing than popular libraries in other languages. Moreover, they also have more opportunity for failure since C is so much less safe than other languages. Even in those few libraries which have received "decades of battle-testing" we still see critical vulnerabilities emerge. Lastly, you're implying a dichotomy between C and JS in a thread about Go, which doesn't have the same dependency sprawl as JS.
Re:Generics can make your Go code slower by dcgudeman on Wednesday March 30, 2022 @02:04PM (#30858667)
Hmm yes, why stop there? Why have functions? Just reimplement business logic all over your codebase. That way each block of code has everything you need to know. Sure, functions have been adopted by every other language/ecosystem and are universally known to be useful despite a few downsides but you could say the same about package management and that hasn't deterred you yet.
Re:Generics can make your Go code slower by dahfizz on Wednesday March 30, 2022 @02:19PM (#30858836)
You're arguing against a straw man. I could just as easily say "why not make every line of code it's own function?"

C has libraries, and my comment made it clear that they are useful.

Argue against my actual point:

By not having a bespoke package manager, and instead relying on the system package manager, you end up with higher quality dependencies and with dramatically less bloat in C than other language ecosystems. It is all the benefit and none of the drawbacks of npm-like ecosystems.

Re:Generics can make your Go code slower by dcgudeman on Wednesday March 30, 2022 @02:47PM (#30859243)
I don't agree with your assessment that libraries in C are higher quality. Additionally I have yet to see a system package manager that enables developers to install dependencies at a specific version solely for a project without a lot of headaches. All the venv stuff in python is necessary python dependencies are installed system wide. The idea that the C/C++ ecosystem is better off because it doesn't have its own package manager is a bizarre idea at best.
Re:Generics can make your Go code slower by imtringued on Thursday March 31, 2022 @05:10AM (#30865247)
I'm one of the few humans on the planet that installs his dependencies inside the project folder and uses PYTHONPATH.
Re:Generics can make your Go code slower by steveklabnik on Wednesday March 30, 2022 @04:39PM (#30860503)
> You end up with much less third party dependencies,

https://wiki.alopex.li/LetsBeRealAboutDependencies

Re:Generics can make your Go code slower by dahfizz on Wednesday March 30, 2022 @10:49PM (#30863347)
This just proves my point. A complex, fully featured GUI application written in C needs 122 libs total.

In Rust, just installing SQLx for sqlite bindings requires 75 crates. And sqlite is one very small part of what rviz does. An rviz equivalent written in rust would require an order of magnitude more dependencies.

Re:Generics can make your Go code slower by pjmlp on Wednesday March 30, 2022 @02:35PM (#30859056)
Ah, that is why all major OSes end up having some form of POSIX support to keep those C applications going.
Re:Generics can make your Go code slower by dahfizz on Wednesday March 30, 2022 @02:45PM (#30859213)
You need have some OS API, what is wrong with POSIX?

And what does POSIX have to do with package management?

Re:Generics can make your Go code slower by pjmlp on Wednesday March 30, 2022 @04:13PM (#30860216)
POSIX is UNIX rebranded as C runtime for OSes that aren't UNIX.
Re:Generics can make your Go code slower by dahfizz on Wednesday March 30, 2022 @10:03PM (#30863088)
I don't even know how to parse that. POSIX is a standard which was based on the unix OS's of the time. POSIX is an API, not a runtime. Only Unix systems today implement posix (MacOs, Linux).
Re:Generics can make your Go code slower by pjmlp on Thursday March 31, 2022 @01:26AM (#30864090)
Yes it is a standard, just like ISO C one is, and traditionally they go go together, just like you never use IP on its own.

I suggest you to have a look around RTOS, mainframes, BeOS, and even NT history regarding POSIX support.

Re:Generics can make your Go code slower by remexre on Wednesday March 30, 2022 @03:16PM (#30859625)
my big personal nit is poor async support; e.g. async disk IO is recent in Linux, and AFAIK all the Unices implement POSIX aio as a threadpool anyway. not being able to wait on "either this mutex/semaphore has been signaled, or this IO operation has completed" is also occasionally very annoying...