7

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?

0

4 Answers 4

18

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Does func somefunc(arg string){} effectively create a somefunc variable? If this is the case, is there any significant difference between them?
No, it does not create a variable so they are different.
But I can do fmt.Println(someFunc) in both way. They both print out the memory address of the function. Is it not technically correct to call something you can pass to fmt.Println as a variable?
Here is what the spec has to say about variable declarations and function declarations. They are distinct but the distinction is small because functions are first-class data in Go.
You can assign a different function to var f = func(){} at runtime, but not to func f(){}. That means the compiler can inline func f(){} at compile time, since it knows f will always refer to the same function.
5
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.

2 Comments

Does func somefunc(arg string){} effectively create a somefunc variable? If this is the case, is there any significant difference between them?
No. In the first case it would be an error to try and assign something else to main. It would be okay in the second case.
4

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.

Comments

0

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()

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.