How To Use The Return Keyword in Go
- In simple terms,
return
is a keyword in Go that is used to end the execution of a function and return a value to the caller of the function. - When a function is executed, it may perform some computations or modify some data, but at some point, it may need to provide a result back to the caller of the function. This is where
return
comes in. - To use
return
in Go, you simply write the keywordreturn
followed by the value that you want to return. Here’s an example:func add(x, y int) int { return x + y }
- In this example, we define a function called
add
that takes two integer argumentsx
andy
. The function returns the sum ofx
andy
using thereturn
keyword followed by the expressionx + y
- In this example, we define a function called
- You can also use
return
to exit a function early if some condition is met. For example:func divide(x, y float64) (float64, error) { if y == 0 { return 0, fmt.Errorf("division by zero") } return x / y, nil }
- In this example, we define a function called
divide
that takes two float64 argumentsx
andy
. The function checks ify
is zero, and if it is, it returns an error using thereturn
keyword followed by the error message.- If
y
is not zero, the function returns the result of dividingx
byy
using thereturn
keyword followed by the expressionx / y
andnil
to indicate that there is no error.
- If
- In this example, we define a function called