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?
To declare a function we use the func keyword. The general structure of a function declaration is shown below. Let’s write a function that prints “Hello World!” in the console. fmt.Println ("Hello World!") // prints "Hello World!" As you can see, calling a function is really easy. Functions in Go can have any return type and any type of argument.
“Defer” is a keyword to delay function call till surrounding function returns. User-defined function types are types that can be identified as a function. Below is an example. Functions in Go can be passed as arguments. This allows us to create Higher Order Functions. A function can return another function as well. Here is how to do that.
By default, Go uses call by value to pass arguments. In general, it means the code within a function cannot alter the arguments used to call the function. The above program, while calling the max () function, used the same method. A function can be used in the following ways:
Since area and perimeter are specified in the function declaration as return values, they are automatically returned from the function when a return statement is encountered. _ is known as the blank identifier in Go. It can be used in place of any value of any type.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With