Skip to content

Instantly share code, notes, and snippets.

@tecnologer
Last active April 18, 2024 17:36
Show Gist options
  • Save tecnologer/9051643d839913294f3570bd9920a022 to your computer and use it in GitHub Desktop.
Save tecnologer/9051643d839913294f3570bd9920a022 to your computer and use it in GitHub Desktop.
Install and configure golangci-lint
linters-settings:
cyclop:
skip-tests: true
tagalign:
align: true
sort: true
order:
- json
- validate
- gorm
- yaml
strict: true
tagliatelle:
# Check the struct tag name case.
case:
use-field-name: true
rules:
json: snake
yaml: camel
linters:
disable-all: true
# Enable specific linters
# https://golangci-lint.run/usage/linters/#enabled-by-default
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- whitespace
- wsl
- misspell
- cyclop
- tagalign
- tagliatelle
# Run only fast linters from enabled linters set (first run won't be fast)
# Default: false
fast: true

Go lanngci-lint

Usage

  1. Download and save linters.sh
    • curl https://gist.githubusercontent.com/Tecnologer/9051643d839913294f3570bd9920a022/raw/21004a7d3f4df68f337f1897cb5987cfb715de65/linters.sh -o ~/linters.sh
  2. Add exec permissinons
    • sudo chmod +x linters.sh
  3. Run linters.sh in the project folder
    • ~/linters.sh
#!/bin/bash
# Function to add GOBIN to PATH in the appropriate shell profile file
add_gobin_to_path() {
# Detect shell and select appropriate profile file
if [[ "$SHELL" == */bash ]]; then
PROFILE_FILE="$HOME/.profile"
elif [[ "$SHELL" == */zsh ]]; then
PROFILE_FILE="$HOME/.zprofile"
else
echo "Unsupported shell. Please add GOBIN to your PATH manually."
return
fi
# Check if GOBIN is already in PATH and add it if not
if [[ ":$PATH:" != *":$GOBIN:"* ]]; then
echo "GOBIN is not in PATH. Adding GOBIN to PATH in $PROFILE_FILE..."
echo "export PATH=\$PATH:\$GOBIN" >> "$PROFILE_FILE"
source "$PROFILE_FILE"
echo "GOBIN has been added to PATH."
else
echo "GOBIN is already in PATH."
fi
}
# Ensure GOBIN is set and exists
if [ -z "$GOBIN" ]; then
echo "GOBIN environment variable is not set. Attempting to set it..."
export GOBIN=$(go env GOPATH)/bin
echo "GOBIN set to $GOBIN"
fi
if [ ! -d "$GOBIN" ]; then
echo "GOBIN directory does not exist. Creating it..."
mkdir -p "$GOBIN"
fi
add_gobin_to_path
# Install Go tools
tools=("github.com/bkielbasa/cyclop/cmd/cyclop@latest"
"github.com/segmentio/golines@latest"
"github.com/golangci/golangci-lint/cmd/golangci-lint@latest"
"github.com/dmarkham/enumer@latest")
for tool in "${tools[@]}"; do
echo "Installing $(echo $tool | cut -d '/' -f 3)..."
go install "$tool"
done
# Create YAML file for linters
curl -sSL https://gist.githubusercontent.com/Tecnologer/9051643d839913294f3570bd9920a022/raw/502a1434cabc1ae0ba8bc9189537bff76d34e545/.golangci.yml -o .golangci.yml
echo "All tools installed and .golangci.yml downloaded."
echo "Note: To configure golines check README at https://github.com/segmentio/golines"
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment