Functions
Defining functions and handling errors
Introduction
In this lesson, you'll learn about functions in Go. Coming from JavaScript, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In JavaScript, you're familiar with defining functions and handling errors.
Go has its own approach to defining functions and handling errors, which we'll explore step by step.
The Go Way
Let's see how Go handles this concept. Here's a typical example:
package main
import (
"errors"
"fmt"
)
func add(a, b int) int { return a + b }
// No default params — use variadic or two functions
func greet(name string) string { return "Hello, " + name }
func greetDefault() string { return greet("World") }
// Variadic (like rest params)
func sum(nums ...int) int {
total := 0
for _, n := range nums {
total += n
}
return total
}
// Multiple return: value + error
func divide(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("division by zero")
}
return a / b, nil
}
result, err := divide(10, 2)
if err != nil {
fmt.Println("error:", err)
}Comparing to JavaScript
Here's how you might have written similar code in JavaScript:
function add(a, b) { return a + b; }
// Default params
function greet(name = "World") {
return "Hello, " + name;
}
// Rest params
function sum(...nums) {
return nums.reduce((a, b) => a + b, 0);
}
// Arrow function
const double = x => x * 2;
// Throws on error
function divide(a, b) {
if (b === 0) throw new Error("division by zero");
return a / b;
}You may be used to different syntax or behavior.
Go returns errors as values; JS throws exceptions
You may be used to different syntax or behavior.
Go has no default parameters; use variadic or overloads
You may be used to different syntax or behavior.
Go multiple return values replace JS's try/catch for errors
You may be used to different syntax or behavior.
Go variadic uses ...T not JS's ...args spread
Step-by-Step Breakdown
1. Error as Return Value
Go's most important pattern: functions return (result, error) instead of throwing. Every caller must check the error.
try { const r = divide(a, b); } catch(e) { ... }r, err := divide(a, b)
if err != nil { /* handle */ }2. No Default Params
Go doesn't support default parameters. Use variadic functions or separate overloaded functions.
function greet(name = "World") { ... }func greet(name string) string { ... }
func greetDefault() string { return greet("World") }3. Variadic Functions
Go's variadic ...T is similar to JS rest params — the parameter becomes a slice inside the function.
function sum(...nums) { return nums.reduce(...) }func sum(nums ...int) int {
total := 0
for _, n := range nums { total += n }
return total
}Common Mistakes
When coming from JavaScript, developers often make these mistakes:
- Go returns errors as values; JS throws exceptions
- Go has no default parameters; use variadic or overloads
- Go multiple return values replace JS's try/catch for errors
Key Takeaways
- Go returns errors; JS throws exceptions
- Multiple returns replace try/catch for expected failures
- No default params — use variadic or multiple functions
- Variadic ...T receives a slice inside the function