JS
GO

JavaScript to Go

10 lessons

Progress0%
1Variables & Types2Functions3Objects → Structs4Async → Goroutines5Errors & Panic6Interfaces7Slices and Maps8Packages and Modules9Testing10Standard Library
All Mirror Courses
JS
GO
Functions
MirrorLesson 2 of 10
Lesson 2

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.

Mirror Card
JS
From JavaScript:

In JavaScript, you're familiar with defining functions and handling errors.

GO
In Go:

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:

GO
Go 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:

JS
JavaScript (What you know)
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;
}
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

Go returns errors as values; JS throws exceptions

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

Go has no default parameters; use variadic or overloads

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

Go multiple return values replace JS's try/catch for errors

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

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.

JS
JavaScript
try { const r = divide(a, b); } catch(e) { ... }
GO
Go
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.

JS
JavaScript
function greet(name = "World") { ... }
GO
Go
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.

JS
JavaScript
function sum(...nums) { return nums.reduce(...) }
GO
Go
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
Common Pitfall
Don't assume Go works exactly like JavaScript. While the concepts may be similar, the syntax and behavior can differ significantly.

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
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in Go to practice these concepts.
PreviousNext