V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
Charlie17Li

[goang nil 检查] golang 有什么好的 nil 检查工具吗

  •  
  •   Charlie17Li · Dec 16, 2024 · 2132 views
    This topic created in 507 days ago, the information mentioned may be changed or developed.

    背景

    golang 新手,写代码时有时候会忘记对 nil 做检查。比如,如下场景:

    1. 变量没有初始化,包括:map ,自定义接口体
    2. 函数没有对入参做 nil 检查
    3. 遇到错误没有 return ,导致后续使用到 nil

    写完这三点意识到这种错误太基础了,但是不过是自己写的,还是别人写的,确实都遇到过以上错误。所以想问的是大家有什么 nil 检查工具。

    调研

    在 github 找到 uber 的 nilaway,试用了下,假阳性太高,不具备可用性,但奇怪为啥 star 那么多(3.2k).

    3 replies    2024-12-17 11:43:04 +08:00
    aduo
        1
    aduo  
       Dec 16, 2024   ❤️ 1
    GoLand 的话, 设置 `Editor | Inspections | Go | Data flow analysis (experimental) | Potential nil dereference` 提示等级到`Error`, 有那么一点用. 搭配 `Qodana analysis` 的话, 可以规避很大部分的新手空指针错误
    soul11201
        2
    soul11201  
       Dec 16, 2024   ❤️ 2
    pike0002
        3
    pike0002  
       Dec 17, 2024
    Uber 开了一个 nilaway 的库,他现在还处于活跃开发期,有大厂背书,关注一下。不过自己之前测试了一下,复杂一点的项目 false positive 还是有点多。https://github.com/uber-go/nilaway
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1355 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 17:06 · PVG 01:06 · LAX 10:06 · JFK 13:06
    ♥ Do have faith in what you're doing.