“已声明和未使用的变量”编译错误

编程入门 行业动态 更新时间:2024-10-25 13:21:13
本文介绍了“已声明和未使用的变量”编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在学习Google的新语言转到。我只是想尝试一些东西,我注意到,如果你声明了一个变量并且不对它做任何事情,那么go编译器(在我的例子中是 8g )失败 - 用这个错误编译: hello.go:9:声明了错误并且未使用。我对此感到惊讶,因为大多数语言编译器只是警告你关于未使用的变量,但仍然可以编译。

I am learning Google's new language Go. I am just trying stuff out and I noticed that if you declare a variable and do not do anything with it the go compiler (8g in my case) fails to compile with this error: hello.go:9: error declared and not used. I was suprised at this since most language compilers just warn you about unused variables but still compile.

有没有办法解决这个问题?我检查了编译器的文档,我没有看到任何会改变这种行为的东西。有没有办法删除 error ,这样就可以编译?

Is there anyway I can get around this? I checked the documentation for the compiler and I don't see anything that would change this behaviour. Is there a way to just delete error so that this will compile?

package main import "fmt" import "os" func main() { fmt.Printf("Hello World\n"); cwd, error := os.Getwd(); fmt.Printf(cwd); }

推荐答案

您可以试试这个:

cwd, _ := os.Getwd();

但是好像保留Jurily答案中的错误会更好,所以你知道出错了。

but it seems like it would be better to keep the error like in Jurily's answer so you know if something went wrong.

更多推荐

“已声明和未使用的变量”编译错误

本文发布于:2023-11-14 03:03:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586038.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   声明   错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!