github.com/klaytn/klaytn@v1.12.1/HOW-TO-LINT-YOUR-CHANGE.md (about)

     1  # How to lint your change
     2  
     3  This document describe how to setup automatically or manually linting your change.
     4  
     5  ## Prerequisites
     6  - go version should be equal to or higher than v1.20.0
     7  - `gofumpt` should be installed. To install it, run `go install mvdan.cc/gofumpt@latest`
     8  - `goimports` should be installed. To install it, run `go install golang.org/x/tools/cmd/goimports@latest`
     9  
    10  ## Git Hook Setup
    11  This will apply code formatting automatically when you commit your change. So you can pass the lint tests registered in Klaytn's CI.
    12  
    13  - Copy and paste below pre-commit script to `klaytn/.git/hooks/pre-commit` file and make the file executable (e.g., `chmod +x pre-commit`).
    14  
    15  ```go
    16  #!/bin/sh
    17  # Copyright 2012 The Go Authors. All rights reserved.
    18  # Use of this source code is governed by a BSD-style
    19  # license that can be found in the LICENSE file.
    20  
    21  # git gofmt pre-commit hook
    22  #
    23  # To use, store as .git/hooks/pre-commit inside your repository and make sure
    24  # it has execute permissions.
    25  #
    26  # This script does not handle file names that contain spaces.
    27  
    28  gofiles=$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$')
    29  [ -z "$gofiles" ] && exit 0
    30  
    31  unformatted=$(gofumpt -l $gofiles)
    32  unimported=$(goimports -l $gofiles)
    33  
    34  # Some files are not gofmt'd. Print message and fail.
    35  echo >&2 "Go files must be formatted with gofmt. Below files are reformatted:"
    36  for fn in $unformatted; do
    37  	echo >&2 "$PWD/$fn"
    38  	gofumpt -w $PWD/$fn
    39  	git add $PWD/$fn
    40  done
    41  
    42  echo >&2 "Algin import packages. Below files are reformatted:"
    43  for fn in $unimported; do
    44  	echo >&2 "$PWD/$fn"
    45  	goimports -w $PWD/$fn
    46  	git add $PWD/$fn
    47  done
    48  
    49  exit 0
    50  ```
    51  
    52  ## Manually formatting
    53  You can format codes manually by using below commands in the root of klaytn repository.
    54  
    55  ```bash
    56  klaytn$ gofumpt -w .
    57  klaytn$ goimports -w .
    58  ```