Go:“变量声明和未使用”编译错误

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

我正在学习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.

有没有我能解决这个问题?我检查了编译器的文档,我没有看到任何会改变这种行为。有没有办法只是删除错误,以便这将编译?

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); }

推荐答案

/ p>

You could try this:

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.

更多推荐

Go:“变量声明和未使用”编译错误

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

发布评论

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

>www.elefans.com

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