douzao9845 2016-12-08 07:23 采纳率: 0%
浏览 161
已采纳

为什么“ go vet”在这里不显示错误?

With the following code, go vet does not show an "out of bounds" error as I would expect:

package main

func main() {
    a := make([]string, 1)
    a[2] = "foo"
}

From the go vet documentation:

Flag: -shift

Shifts equal to or longer than the variable's length.

If go vet is not the tool to catch these errors, what is? Compiling and/or testing the code will catch this, but I'm looking for a static analysis based tool.

  • 写回答

1条回答 默认 最新

  • doushui3216 2016-12-08 07:53
    关注

    Its true that Go vet is for catching suspicious runtime error, by using some heuristics. The first Para is exact regarding its work

    Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string. Vet uses heuristics that do not guarantee all reports are genuine problems, but it can find errors not caught by the compilers.

    also in documentation its mentioned that

    Note that the tool does not check every possible problem and depends on unreliable heuristics.

    also the code which you are using to check for vetting your package is something very difficult to find by those heuristics as you are using a dynamic slice which can be appended or modified at runtime. thereby not a perfect heuristic can be thought about for that.

    fmt.Printf("%d", "scsa", "DSD")
    

    those heuristic can catch things like this it all depends on what the training data is.

    So vet should be a tool to take a quick look whether there is some general mistake which has been missed by you (If It gets caught :-) )its nothing like a compile tool or runtime checker it just runs some heuristics on the plane code you have written.

    also documentation provides a list of available checks some examples are

    Assembly declarations, Copying locks, Printf family, Methods, Struct tags,

    etc there are many, you can see and read the complete documentation

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥20 ERR_CACHE_MISS 确认重新提交表单
    • ¥20 关于vba使用HTMLfile执行js函数问题
    • ¥60 悬赏求解,通过实时现场摄像头的视频图像识别其他对家打出的麻将牌,识别麻将牌,识别牌墙位置,通过识别对家打出了什么牌
    • ¥15 关于#GPU jetson#的pcie驱动开发问题,如何解决?
    • ¥15 stm32f103zet6 串口5无法收发数据
    • ¥15 关于C语言使用线程队列实现多线程并发
    • ¥15 这个运行,错误在哪里呀,大家看看吧,教导我
    • ¥15 advanceinstaller对话框设置
    • ¥100 正常上网,内部网页无法打开
    • ¥15 组件库引入并使用在若依框架未展示