News for hackers. Stuff that matters.
Re:Scripting with Go: A Modest Proposal by simonw on Sunday August 20, 2023 @10:19AM (#37199385)
If you're not familiar with Go there is one detail missing from this post (though it's in the script README) - what a complete program looks like. Here's the example from https://github.com/bitfield/script#a-realistic-use-case

    package main

    import (
        "github.com/bitfield/script"
    )

    func main() {
        script.Stdin().Column(1).Freq().First(10).Stdout()
    }
Re:Scripting with Go: A Modest Proposal by alexk307 on Sunday August 20, 2023 @10:28AM (#37199486)
The whole point of using Go is to explicitly handle errors as they happen. All of these steps can fail, but it’s not clear how they fail and if the next steps should proceed or be skipped on previous failures. This is harder to reason about, debug, and write than grep and bash.
Re:Scripting with Go: A Modest Proposal by mst on Sunday August 20, 2023 @10:46AM (#37199643)
It defaults to not running the rest if a step fails, and the error result is accessible via usual mechanisms.

  _, err := script.Foo(...).Bar(...).Stdout();
  if err != nil {
    log.Fatal(err)
  }
is sufficient for a quick scripting hack designed to be run interactively.

I don't see it as a lot different to bash scripts with -e and pipefail set, which is generally preferable anyway.

Plenty of go code does

  if err != nil {
    return nil, err;
  }
for each step and there are plenty of cases where you only care -if- it failed plus a description of some sort of the failure - if you want to proceed on some errors you'd split the pipe up so that it pauses at moments where you can check that and compensate accordingly.

(and under -e plus pipefail, "error reported to stdout followed by aborting" is pretty much what you get in bash as well, so I'm unconvinced it's actually going to be harder to debug)

Re:Scripting with Go: A Modest Proposal by simonw on Sunday August 20, 2023 @11:03AM (#37199844)
I believe error handling looks like this:

    package main

    import (
        "github.com/bitfield/script"
    )

    func main() {
         _, err := script.Stdin().Column(1).Freq().First(10).Stdout()
        if err != nil {
            log.Fatal(err)
        }
    }
Errors are "remembered" by the pipeline and can be processed when you get to a sink method.
Re:Scripting with Go: A Modest Proposal by coldtea on Sunday August 20, 2023 @11:52AM (#37200293)
>The whole point of using Go is to explicitly handle errors as they happen

That's hardly the whole point of using Go.

The friendlier syntax (and in this case DSL) is an ever bigger point.

In any case, you can trivially get at the error at the point it occured:

n, err := script.File("test.txt").Match("Error").CountLines()

Re:Scripting with Go: A Modest Proposal by fpoling on Sunday August 20, 2023 @02:19PM (#37201654)
From a technical point of view nothing prevents the scripting package to be just as informative with errors as bash and have a helper to log and clear the error. If it is not already the case, I call it a bug.
Re:Scripting with Go: A Modest Proposal by jvictor118 on Sunday August 20, 2023 @11:03AM (#37199848)
If one were actually going to use something like this, I’d think it’d be worth implementing a little shebang script that can wrap a single-file script in the necessary boilerplate and call go run!
Re:Scripting with Go: A Modest Proposal by simonw on Sunday August 20, 2023 @11:42AM (#37200202)
That's a really fun idea. I got that working here: https://til.simonwillison.net/bash/go-script

Now you can run this:

    cat file.txt | ./goscript.sh -c 'script.Stdin().Column(1).Freq().First(10).Stdout()'
Or write scripts like this - call it 'top10.sh':

    #!/tmp/goscript.sh
    script.Stdin().Column(1).Freq().First(10).Stdout()
Then run this:

    chmod 755 topten.sh
    echo "one\none\ntwo" | ./topten.sh
Re:Scripting with Go: A Modest Proposal by moondev on Sunday August 20, 2023 @10:25PM (#37204834)
This is fantastic. Thanks for sharing and also including docs on your process and usage!
Re:Scripting with Go: A Modest Proposal by jayd16 on Sunday August 20, 2023 @02:23PM (#37201693)
Hmm, I wonder if this is Microsoft's real endgame with allowing the single line C# syntax.