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 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型