英文:
getting an error in go: func main is unused
问题
我在Go语言中编写了一段简单的代码,但是出现了一个奇怪的错误。我已经附上了代码和错误的截图。
错误信息:func main is unused
代码:
package structs
import "fmt"
func main() {
fmt.Println("Hello Structs")
}
截图:
英文:
I have written a simple code in Go but I am getting a weird error. I have attached a screenshot of code and error.
error: func main is unused
Code:
package structs
import "fmt"
func main() {
fmt.Println("Hello Structs")
}
Screenshot:
答案1
得分: 19
将package structs
更改为package main
。
https://golang.org/ref/spec#Program_execution
> 通过将一个名为main package的未导入包与其直接或间接导入的所有包链接,可以创建一个完整的程序。主包必须具有包名main
并声明一个不带参数且不返回值的main
函数。
请注意,func main is unused
本身并不是一个错误,它只是来自go-staticcheck
linter的未使用代码的报告。未使用的函数是允许的,但是如果我没有弄错的话,它们将被省略在输出的二进制文件中。
英文:
Change package structs
to package main
.
https://golang.org/ref/spec#Program_execution
> A complete program is created by linking a single, unimported package
> called the main package with all the packages it imports,
> transitively. The main package must have package name main
and declare
> a function main
that takes no arguments and returns no value.
Note that func main is unused
by itself is not an error, it is just a report of an instance of unused code from the go-staticcheck
linter. Unused functions are allowed by the Go compiler, but they will be, if I'm not mistaken, omitted from the output binary.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论