在Go语言中遇到了一个错误:func main未使用。

huangapple go评论434阅读模式
英文:

getting an error in go: func main is unused

问题

我在Go语言中编写了一段简单的代码,但是出现了一个奇怪的错误。我已经附上了代码和错误的截图。

错误信息:func main is unused

代码:

package structs

import "fmt"

func main() {
	fmt.Println("Hello Structs")
}

截图:

在Go语言中遇到了一个错误:func main未使用。

英文:

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:

在Go语言中遇到了一个错误:func main未使用。

答案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.

huangapple
  • 本文由 发表于 2021年8月18日 17:41:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/68830004.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定