1

I am using go programming

I have structs like this:

type person struct {  
   firstname string  
   lastname string  
   age int  
   address string  

}  

I want it to be aligned like this

type person struct {
   firstname string
   lastname  string
   age       int
   address   string

}  

and stuff like

const (  
    constant1 int = 1  
    c2 int = 2  
    const3 int = 3  
)    

to

const (    
    constant1   int = 1  
    c2          int = 2  
    const3      int = 3  
)   

I know align-regexp should help here, but i am unable to figure it out.

1

M-x gofmt does this. Here is the output applied to your examples.

package main

type person struct {
    firstname string
    lastname  string
    age       int
    address   string
}

const (
    constant1 int = 1
    c2        int = 2
    const3    int = 3
)

gofmt runs the external command on the buffer. Here is the output of the external command:

package main

type person struct {
    firstname string
    lastname  string
    age       int
    address   string
}

const (
    constant1 int = 1
    c2        int = 2
    const3    int = 3
)

This is with Go version go1.11.1.

You could also apply the gofmt command yourself using shell-command-on-region, which has nothing to do with the Emacs go mode and should show you whether the command itself works or not.

|improve this answer|||||
  • does not work, gofmt does not fix the alignment for me – Pharaoh Nov 2 '18 at 8:42
  • gofmt runs the external gofmt program on the buffer. What happens if you run gofmt on the code, not via Emacs but as a standalone? – Dusty Nov 2 '18 at 10:05
  • when i run it using M-x gofmt, it says buffer is already gofmted, it removes remove empty lines and everything, but does not do the alignment – Pharaoh Nov 2 '18 at 10:25
  • Hmm. The standalone gofmt which comes with go1.11.1 does the alignment as shown in my post, except with tabs. What version are you using? – Dusty Nov 2 '18 at 11:56
  • Mine is go version 1.10.3 – Pharaoh Nov 2 '18 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.