1

I've read that Go uses

  • tabs for indentation
  • spaces for alignment

and this sounds like a great idea to resolve the tabs-versus-spaces fight and get the benefits that tabs promise without the problems they can create, BUT I cannot find any practical way to implement this in editors with .editorconfig.

There is no setting I can find that will cause my editor to emit tab characters when I hit tab at the beginning of a line (when only whitespace is on that line) but space characters when there are other (non-whitespace) characters earlier on the same line.

(And NO I am not going to whack on the space bar to enter spaces for alignment. There is too much muscle memory in using the tab key, and the tiny benefit of using tabs for indentation would be more than lost a hundred times over compared to the need to whack the space key over and over and over and over and over and over...)

I use Zed, but the best answer will tell us how to do this in a way that works in all popular editors, such as by using .editorconfig. That might be impossible, in which case the best answer will include directions for multiple popular editors.

(As an aside, I'm curious to know how many Go programmers actually follow this convention.)

Tangential Update on Why .editorconfig Might Never Do This

Skip this if you don't like tangents.

While looking at some C code related to Futhark, I realized that some people align code in a way that might make it (nearly?) impossible for their editor to distinguish between indentation and alignment. I never do that (I think it's a horrible practice) but that might explain why the creators of .editorconfig didn't try to solve for the scenario I described. Here's an example:

int futhark_unpack(struct futhark_context *ctx,
                   struct futhark_i32_1d **out0,
                   struct futhark_i32_1d **out1,
                   struct futhark_opaque_z31U814583239044437263 *in0);

1 Answer 1

2

The best option I can find is to run go fmt on save instead of relying on .editorconfig. For Zed that is done by editing settings.json:

  "languages": {               // this line given for context
    "Go": {
      "format_on_save": "on"
    },

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.