golang检测源代码中非包容性语言的插件woke的使用
Golang检测源代码中非包容性语言的插件woke的使用
woke是一个用于检测源代码中非包容性语言的工具,它会扫描代码并标记出可能包含歧视性或非包容性的词汇,同时提供更包容的替代方案建议。
什么是woke
woke是一个文本文件分析工具,能够检测源代码中的非包容性语言并提出更包容的替代方案。创建包容性工作环境对于健康、支持和高效的文化至关重要。
安装woke
可以通过以下方式安装woke:
# 使用go install安装
go install github.com/get-woke/woke@latest
# 或者使用brew安装(macOS)
brew install get-woke/tap/woke
使用示例
下面是一个完整的Golang代码示例,展示如何使用woke检测代码中的非包容性语言:
package main
import (
"fmt"
"log"
"os/exec"
)
func main() {
// 使用woke检测当前目录下的代码
cmd := exec.Command("woke", ".")
// 捕获输出
output, err := cmd.CombinedOutput()
if err != nil {
// woke会在发现非包容性语言时返回非零状态码
// 所以这里的错误可能是预期中的
log.Printf("woke执行完成,发现非包容性语言")
}
// 打印检测结果
fmt.Println(string(output))
// 示例输出可能包含:
// main.go:10:5: `whitelist` may be non-inclusive, use `allowlist` instead
}
配置woke
woke支持配置文件.woke.yml
,可以自定义规则和忽略文件:
# .woke.yml 示例配置
rules:
- name: master-slave
alternatives:
- primary-secondary
- leader-follower
note: "Master/slave terminology is discouraged as it perpetuates negative stereotypes"
severity: warning
ignore:
- "vendor/**"
- "**/testdata/**"
在CI/CD中集成
可以在CI/CD流程中添加woke检查,例如在GitHub Actions中:
name: woke check
on: [push, pull_request]
jobs:
woke:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run woke
uses: get-woke/woke-action@v1
为什么使用woke
许多知名公司如GitHub、Twitter和Apple都在积极支持向包容性语言的转变。woke可以帮助您的团队:
- 识别代码中的潜在非包容性语言
- 提供更包容的替代方案建议
- 在开发流程早期发现问题
- 促进更包容的开发文化
woke是MIT许可的开源项目,由Caitlin Elfring创建并维护。
更多关于golang检测源代码中非包容性语言的插件woke的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
更多关于golang检测源代码中非包容性语言的插件woke的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
使用woke检测Go代码中的非包容性语言
woke是一个开源的静态分析工具,专门用于检测源代码中的非包容性语言(如种族歧视、性别歧视等词汇)。下面我将介绍如何在Go项目中使用woke。
安装woke
首先安装woke工具:
go install github.com/get-woke/woke@latest
或者使用brew(macOS):
brew install woke
基本使用
最简单的使用方式是直接运行woke扫描当前目录:
woke .
这会扫描当前目录下的所有文件,并报告潜在的非包容性语言。
在Go项目中的集成
1. 创建配置文件
在项目根目录创建.woke.yml
配置文件:
rules:
- name: master-slave
terms:
- master
- slave
alternatives:
- primary-replica
- main-secondary
severity: error
- name: blacklist-whitelist
terms:
- blacklist
- whitelist
alternatives:
- denylist-allowlist
- blocklist-passlist
severity: warning
- name: sanity-check
terms:
- sanity
alternatives:
- quick-check
- smoke-test
severity: warning
2. 集成到CI/CD
可以在项目的Makefile中添加:
lint:
woke -c .woke.yml ./...
或者直接在GitHub Actions中使用:
name: Woke Lint
on: [push, pull_request]
jobs:
woke:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '1.19'
- run: go install github.com/get-woke/woke@latest
- run: woke -c .woke.yml ./...
3. 预提交钩子
可以添加pre-commit钩子自动检查:
#!/bin/sh
echo "Running woke..."
woke -c .woke.yml ./...
if [ $? -ne 0 ]; then
echo "Found non-inclusive language, please fix before committing"
exit 1
fi
自定义规则示例
以下是一个更完整的自定义规则配置示例:
include:
- default
rules:
- name: gendered-pronouns
terms:
- he/she
- him/her
- his/hers
alternatives:
- they
- them
- theirs
severity: warning
note: "Use gender-neutral pronouns when possible"
- name: man-hours
terms:
- man hours
- man-hours
alternatives:
- person-hours
- engineer-hours
severity: warning
- name: dummy
terms:
- dummy
alternatives:
- placeholder
- sample
severity: warning
exclude:
- "**/vendor/**"
- "**/testdata/**"
- "**/*_test.go"
在Go代码中的替代方案
当woke检测到问题时,可以考虑以下替代方案:
master/slave
→primary/replica
或leader/follower
blacklist/whitelist
→denylist/allowlist
或blocklist/passlist
sanity check
→quick check
或smoke test
dummy
→placeholder
或sample
man hours
→person hours
或engineering hours
高级用法
仅显示错误
woke --fail-on error .
输出JSON格式
woke --output json .
排除特定文件
woke --exclude "**/vendor/**" .
总结
woke是一个简单但强大的工具,可以帮助团队维护更具包容性的代码库。通过在CI/CD流程中集成woke,可以确保代码审查过程中自动检查非包容性语言,从而创建更友好的开发环境。
记住,工具只是辅助,更重要的是培养团队的包容意识。在修复woke指出的问题时,可以与团队讨论最合适的替代词汇,确保既保持包容性又不影响代码可读性。