gpt4 book ai didi

Golang func main() 在 main 以外的包中?

转载 作者:IT王子 更新时间:2023-10-29 01:34:45 26 4
gpt4 key购买 nike

helloworld为例,包名是main,还有func main()语句。但是我也看到了 func main() 的代码,它在其他一些包中。然而,该代码似乎可以用作独立程序。那么,func main() 的包语句不是 package main 是什么意思?

最佳答案

Go 编程语言由其规范定义。


The Go Programming Language Specification

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.

func main() { … }

Program execution begins by initializing the main package and then invoking the function main. When that function invocation returns, the program exits. It does not wait for other (non-main) goroutines to complete.


函数名mainpackage main之外没有特殊意义。

关于Golang func main() 在 main 以外的包中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50706238/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com