Golang静态代码扫描
1. gitlab中配置静态代码检查 在Go语言中,可以使用一些第三方工具来进行静态代码扫描,常用的工具包括: Go vet:Go语言官方提供的工具,用于检查代码中的常见错误和问题; oLint:静态分析工具,可以检查代码中的一些不规范的写法和风格; GoMetaLinter:Go语言的多工具静态代码分析器,可以集成多种静态分析… 阅读更多 »Golang静态代码扫描
1. gitlab中配置静态代码检查 在Go语言中,可以使用一些第三方工具来进行静态代码扫描,常用的工具包括: Go vet:Go语言官方提供的工具,用于检查代码中的常见错误和问题; oLint:静态分析工具,可以检查代码中的一些不规范的写法和风格; GoMetaLinter:Go语言的多工具静态代码分析器,可以集成多种静态分析… 阅读更多 »Golang静态代码扫描
1. 问题 go1.16之前不使用第三方包前提下实现如下功能是比较困难的 编译后的二进制文件和ini/toml/yaml格式的配置文件必须同时存在,仅移动二进制文件可能就跑不起来了 开发一个简单的http服务,引入了js、css、html文件最终需要与go源码编译后的二进制文件保证正确的文件路径结构,然后一起压缩成一个文件后才能… 阅读更多 »Golang嵌入静态文件embed(转载)
1. 第一种方法:工具调试 1.1. ps -ef 查看进程号
1 2 3 4 |
[root@local]# ps -ef |grep test root 967496 967456 99 09:57 ? 00:06:52 /usr/bin/test-controller-cn -conf /etc/tester/config.yaml root 970312 964216 0 09:59 pts/29 00:00:00 grep --color=auto test |
1.1. ps -eLF 查看进程对应线程,采用的CPU核心号 -e 或者-A,选择所有的进程 L 显示线程信息 F 显示该线程使用CPU核心序号 [crayon-6481d0d671ba438287… 阅读更多 »Golang程序性能排查
1. dlv的安装 delve,简称dlv是go语言的最常用的调试器,获取dlv的操作命令如下:
1 2 3 |
go get -u github.com/derekparker/delve/cmd/dlv #验证dlv安装 dlv version |
安装后直接运行dlv将会看到如下信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
[root@localhost ~]# dlv Delve is a source level debugger for Go programs. Delve enables you to interact with your program by controlling the execution of the process, evaluating variables, and providing information of thread / goroutine state, CPU register state and more. The goal of this tool is to provide a simple yet powerful interface for debugging Go programs. Pass flags to the program you are debugging using `--`, for example: `dlv exec ./hello -- server --config conf/config.toml` Usage: dlv [command] Available Commands: attach Attach to running process and begin debugging. connect Connect to a headless debug server. core Examine a core dump. dap [EXPERIMENTAL] Starts a headless TCP server communicating via Debug Adaptor Protocol (DAP). debug Compile and begin debugging main package in current directory, or the package specified. exec Execute a precompiled binary, and begin a debug session. help Help about any command run Deprecated command. Use 'debug' instead. test Compile test binary and begin debugging program. trace Compile and begin tracing program. version Prints version. Flags: --accept-multiclient Allows a headless server to accept multiple client connections. --allow-non-terminal-interactive Allows interactive sessions of Delve that don't have a terminal as stdin, stdout and stderr --api-version int Selects API version when headless. New clients should use v2. Can be reset via RPCServer.SetApiVersion. See Documentation/api/json-rpc/README.md. (default 1) --backend string Backend selection (see 'dlv help backend'). (default "default") --build-flags string Build flags, to be passed to the compiler. For example: --build-flags="-tags=integration -mod=vendor -cover -v" --check-go-version Exits if the version of Go in use is not compatible (too old or too new) with the version of Delve. (default true) --disable-aslr Disables address space randomization --headless Run debug server only, in headless mode. -h, --help help for dlv --init string Init file, executed by the terminal client. -l, --listen string Debugging server listen address. (default "127.0.0.1:0") --log Enable debugging server logging. --log-dest string Writes logs to the specified file or file descriptor (see 'dlv help log'). --log-output string Comma separated list of components that should produce debug output (see 'dlv help log') --only-same-user Only connections from the same user that started this instance of Delve are allowed to connect. (default true) -r, --redirect stringArray Specifies redirect rules for target process (see 'dlv help redirect') --wd string Working directory for running the program. Additional help topics: dlv backend Help about the --backend flag. dlv log Help about logging flags. dlv redirect Help about file redirection. Use "dlv [command] --help" for more information about a command. |
2. 基础命令 上面列举了dlv的一些命令,其中常用的有如h… 阅读更多 »Golang的Dlv调试
1. 概述 前面介绍了golang的一般单元测试,以及如何使用vscode进行高效的go单元测试开发。同时也说过一般单元测试重点在于cpu和内存类型的测试,而对io类型的测试是比较敏感的。那么针对这类测试就没法做单元测试了吗?有的,肯定是有的,这就是mock技术。 mock测试不但… 阅读更多 »Golang Mock使用入门
golangci-lint安装及运行
1 2 3 4 5 6 7 8 9 |
go get -u github.com/golangci/golangci-lint pushd ${GOPATH}/pkg/mod/github.com/golangci/golangci-lint@v1.41.1/cmd/golangci-lint go build cp golangci-lint /usr/bin popd #运行 golangci-lint run |
code_check.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#!/usr/bin/env bash function Fail() { echo "Abort with FAILs." exit 1 } echo -e "Process go vet...\c" go vet $(go list ./... | grep -v /vendor/) || Fail echo "PASS" echo -e "Process ci-lint...\c" which golangci-lint 1>/dev/null || (echo "Installing golangci-lint" && go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.32.2) golangci-lint run --timeout 10m || Fail echo "PASS" echo -e "Process go test...\c" go test $(go list ./... | grep -v /vendor/) -timeout=10s || Fail echo "PASS" echo "All check passed." |
Go中Channel发送和接收操作指南 1. 前言 先来看一道面试题: 对已经关闭的 chan 进行读写,会怎么样?为什么? 在golang中channel属于较为核心的一个功能,尤其在go协程中,channel功能尤为重要;如果多个任务之间需要通信,可能就要用到通道(channel)。 2. Channel的定义 声明并初始化… 阅读更多 »Golang Channel发送和接收