Function declaration in Golang

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

Function declaration in Golang

问题

在Golang中,声明函数有两种不同的方式,如下所示:

package main
import "fmt"
var someFunc = func(arg string) {
    fmt.Println(arg)
}
func main() {
    someFunc("Hello")
}

上面的代码是可以工作的。然而,下面的代码是无法工作的:

package main
import "fmt"
var someFunc = func(arg string) {
    fmt.Println(arg)
}
var main = func() {
    someFunc("Hello")
}

它会报错:

runtime.main: undefined: main.main

那么func someFunc()var someFunc = func()之间有什么区别呢?

我发现这个问题可能是因为我也写了很多JavaScript代码。在Go中,我很少看到人们像var someFunc=func()这样声明函数。在这两种方式中,我们可以说哪一种比另一种更正确吗?

英文:

It seems there are two different ways to declare a function in Golang, like this:

package main
import "fmt"
var someFunc = func(arg string) {
	fmt.Println(arg)
}
func main() {
	someFunc("Hello")
}

The above works. However, the below does not work:

package main
import "fmt"
var someFunc = func(arg string) {
	fmt.Println(arg)
}
var main = func() {
	someFunc("Hello")
}

It will complain:

runtime.main: undefined: main.main

So what's the difference between func someFunc() and var someFunc = func()?

The reason I found this is probably because of I code a lot of Javascript, too. It seems in Go, I rarely see people declaring a function like var someFunc=func(). Of these two, can we say which one is more correct than the other one?

答案1

得分: 18

当你这样写的时候:

var someFunc = func(arg string) {}

你是将一个匿名函数赋值给了 someFunc 变量。你也可以这样写:

someFunc := func(arg string) {}

另一种创建函数的方式是创建一个命名函数:

func someFunc(arg string) {}

命名函数只能在顶层声明,而匿名函数可以在任何地方声明。而且 main 有特殊的含义,在 main 包中必须有一个名为 main 的命名函数,这就是为什么你在第二种情况下会得到一个错误。

英文:

When you do

var someFunc = func(arg string) {}

you are assigning an anonymous function to the somefunc variable. You could also write it like this:

somefunc := func(arg string) {}

The other way to create a function is to create a named function:

func somefunc(arg string) {}

Named functions can only be declared at the top level whereas anonymous functions can be declared anywhere. And main has a special meaning, there has to be a named function called main in the main package, that's why you got an error in the second case.

答案2

得分: 5

主函数(main)的声明。

这是声明一个匿名函数,并将其赋值给名为 main 的变量。在Go语言中,函数是一等公民,你可以将函数本身赋值给一个变量。

英文:
func main() {

This is declaring a function named main.

var main = func() {

This is declaring an anonymous function and assigning it to a variable called main. Functions are first-class data in Go. You can assign the function itself to a variable.

答案3

得分: 4

这是一个函数声明:

func main() {}

这是一个变量声明:

var main = func() {}

语言规范中说:

> "主包必须具有包名main并声明一个不带参数且不返回值的函数main。"

变量声明不是函数声明,因此不符合main函数的要求。

英文:

This is a function declaration:

func main() {}

This is a variable declaration:

var main = func() {}

The language specification says:

> "The main package must have package name main and declare a function
> main that takes no arguments and returns no value."

A variable declaration isn't a function declaration and therefore doesn't meet the requirements for main.

答案4

得分: 0

它在抱怨,因为所有的函数都必须在主函数内部调用,而主函数是程序的起始点。

例如:

package main

import "fmt"

func Print() {

    fmt.Println("Print function")
}

func main() {

    Print()

}
英文:

It is complaining because all functions have to be called inside the main function which is the starting point of your program.

Ex:

package main

import "fmt"

func Print() {

	fmt.Println("Print function")
}

func main() {

	Print()

}

huangapple
  • 本文由 发表于 2014年5月29日 10:56:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/23925005.html
匿名

发表评论

匿名网友

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

确定