Coder Social home page Coder Social logo

go_fmt's Introduction

Go 代码格式化

1.功能说明

  • 格式化import部分:分3段式,依次为 标准库第三方库项目自身库
  • 格式化单行注释:若为 //注释内容,调整为 //{空格}注释内容
  • 默认只对git项目库里有修改的进行格式化
  • 支持将多行的 copyright 注释修改为单行格式(默认不调整)

对于import部分:

1.可使用-mi参数来控制是否将多段import合并为一段(默认否)。
2.对于注释的import path,会按照其实际路径参与分组和排序。
3.对于非末行的注释的位置会和其下面紧挨的import path绑定在一起。
4.末行的注释则会放入import的头部。
5.import path 不要使用相对路径(如./../)。

会忽略当前目录以及子目录下的testdatavendor目录。
若需要可进入其目录里执行该命令。

2.安装/更新

使用 go >= 1.13 版本(不需要Fan强):

export GO111MODULE=on
go env GOPROXY=https://goproxy.cn,direct

go get -u github.com/fsgo/go_fmt

go < 1.13 版本(可能需要Fan强):

go get -u github.com/fsgo/go_fmt

3.使用

3.0 help

go_fmt -help

usage: go_fmt [flags] [path ...]
  -local string
    	put imports beginning with this string as 3rd-party packages (default "auto")
  -mi
    	merge imports into one
  -slcr
    	multiline copyright to single-line
  -trace
    	show trace infos
  -w	write result to (source) file instead of stdout (default true)

3.1 格式化git项目里有修改的.go文件

$ go_fmt

3.2 对当前目录所有.go文件格式化

$ go_fmt ./...

3.3 对指定.go文件格式化

$ go_fmt abc.go

4.配置到git hooks(commit前自动格式化代码)

4.1 配置项目Hooks

编辑项目的 .git/hooks/pre-commit文件,将go_fmt命令加入。

方式1:

echo -e '\ngo_fmt\n' >> $(git rev-parse --git-dir)/hooks/pre-commit
chmod 777 $(git rev-parse --git-dir)/hooks/pre-commit

方式2:

wget https://raw.githubusercontent.com/fsgo/go_fmt/master/pre-commit -O $(git rev-parse --git-dir)/hooks/pre-commit
chmod 777 $(git rev-parse --git-dir)/hooks/pre-commit

4.2 配置到全局Hooks

该方式会导致项目自身的hooks失效。
若项目有自己的hooks,请不要配置全局而要配置到单个项目。

mkdir -p ~/.git_config/hooks/
git config --global core.hooksPath ~/.git_config/hooks/

wget https://raw.githubusercontent.com/fsgo/go_fmt/master/pre-commit -O ~/.git_config/hooks/pre-commit
chmod 777 ~/.git_config/hooks/pre-commit

go_fmt's People

Contributors

hidu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.