gpt4 book ai didi

go - 包 "main"和功能 "main"

转载 作者:IT老高 更新时间:2023-10-28 13:04:20 30 4
gpt4 key购买 nike

我看过并尝试过的 intro/sample go progs 开始于

package main

并且拥有

func main()

package 行中的“main”和 func 行中的“main”有什么关系吗?我猜不是。 C/C++ 使用相同的“主”入口点。只是想确定一下。我还没有看到任何文档说使用“main”只是巧合。

最佳答案

应用程序的入口点是 main 包中的 main 函数as described in the specification :

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.

语言规范没有赋予此上下文之外的名称 main 特殊含义。名称 main 不是保留名称。

可以在非主包中声明一个main函数。在这种情况下,它只是一个名为 main 的函数。

关于go - 包 "main"和功能 "main",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42333488/

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