[Introduction]变量未使用问题

525 阅读1分钟

变量未使用

变量只作为左值赋值是无法通过编译的:

func main() {
    var n int // “n 已声明且未使用”
    n = 5     // 这也没用,还是会编译报错
}

使用_赋值屏蔽这个问题:

func main() {
        var n int
        n = 5
        _ = n // n 现在被使用了
}

参考链接

Go 报错:变量未使用 —— xxx declared and not used