This question already has an answer here:
I am new to go and have come across a set of parens I don't understand.
In the code below I am using a basic defer statement to print the output of a panic.
To get the code to work I have to add an open and close paren after the end of my functions closing curly brace.
Clearly I don't understand something properly. Am I incorrect in thinking that the two lines above it are the body of the function? If they are the body of the function then what purpose do the parens after it serve. If they are not the body then can someone explain what is happening here?
the error I get without the parens is:
# command-line-arguments
./goTemplate.go:14: syntax error: argument to go/defer must be function call
.
package main
import "fmt"
func main(){
defer func() {
str := recover()
fmt.Println(str)
}()
fmt.Println("Hello, World")
panic("PANIC")
}
</div>