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

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

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?