简体   繁体   English

Emacs Go Lang结构对齐文本

[英]Emacs Go Lang Struct Align Text

In go we 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. 我知道align-regexp应该在这里有帮助,但是我无法弄清楚。

Have you tried go-mode ? 您是否尝试过go-mode It will use fmt or goimport to format you code. 它将使用fmtgoimport格式化您的代码。

是的,我正在使用go-mode和go-fmt都未对齐

go-mode should run gofmt or goimports if you put it in the save hook. 如果将它放在保存钩子中,则go-mode应该运行gofmtgoimports

(use-package go-mode
  :config
  (when (executable-find "goimports")
    (setq gofmt-command "goimports"))
  (defun jpk/go-mode-hook ()
    (add-hook 'before-save-hook #'gofmt-before-save nil 'local))
  (add-hook 'go-mode-hook #'jpk/go-mode-hook)
  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM