-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Open
Description
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
returnkeyword
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
returnis still required - Add the missing
returnstatement to make the code compile and run - Update the explanation to clarify that named return values only omit the variable name, not the keyword
Metadata
Metadata
Assignees
Labels
No labels