Issue Location
Go Functions Tutorial - Named Return Values Example (https://www.learn-golang.org/en/Functions)
Problem
The tutorial contains an example code showing how to use named return values:
func add (a int, b int) (sum int) {
sum = a + b // so no need for a return statement, go takes care of it
}
This code has TWO critical problems:
- Misleading Comment: The comment states "so no need for a return statement, go takes care of it" - This is FALSE
- Missing Return Statement: The code is missing the required
return keyword
Compilation Error
When executed, this code produces:
missing return at end of function
Root Cause
While named return values allow you to omit the variable name in the return statement (e.g., return instead of return sum), you still MUST have the return keyword.
Correct Code
func add (a int, b int) (sum int) {
sum = a + b
return // The return keyword is REQUIRED
}
Impact
This misleading comment and non-working example:
- Confuses learners about Go's function requirements
- Causes runtime compilation errors
- Violates the tutorial's educational goal of teaching correct Go syntax
Recommended Fix
- Remove the misleading comment or correct it to explain that
return is still required
- Add the missing
return statement to make the code compile and run
- Update the explanation to clarify that named return values only omit the variable name, not the keyword
Issue Location
Go Functions Tutorial - Named Return Values Example (https://www.learn-golang.org/en/Functions)
Problem
The tutorial contains an example code showing how to use named return values:
This code has TWO critical problems:
returnkeywordCompilation Error
When executed, this code produces:
Root Cause
While named return values allow you to omit the variable name in the return statement (e.g.,
returninstead ofreturn sum), you still MUST have thereturnkeyword.Correct Code
Impact
This misleading comment and non-working example:
Recommended Fix
returnis still requiredreturnstatement to make the code compile and run