JS
GO

JavaScript to Go

10 lessons

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

Variables & Types

Static typing and variable declaration

Introduction

In this lesson, you'll learn about variables & types 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 static typing and variable declaration.

GO
In Go:

Go has its own approach to static typing and variable declaration, 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

var name string = "Alice"
var age int = 30
var score float64 = 9.5
var active bool = true

const Max = 100

// Short declaration (inside functions)
city := "Istanbul" // type inferred

// Multiple assignment
x, y := 1, 2
a, b := b, a // swap!

Comparing to JavaScript

Here's how you might have written similar code in JavaScript:

JS
JavaScript (What you know)
let name = "Alice";
let age = 30;
let score = 9.5;
let active = true;

const MAX = 100;

// Destructuring
const [first, second] = [1, 2];
const { x, y } = point;
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

Go is statically typed; types cannot change after declaration

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

Go := is short declaration with inference; used inside functions

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

Go has separate int and float64 types; JS has just number

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

Go no destructuring syntax; use multiple assignment x, y := 1, 2

Step-by-Step Breakdown

1. Short Declaration

Go's := declares and initializes with type inference — simpler than JS let/var but types are still fixed.

JS
JavaScript
let city = "Istanbul";
GO
Go
city := "Istanbul" // string inferred, fixed

2. Numeric Types

Go has separate int and float64 types. Unlike JS's single number type, Go integers and floats are distinct.

JS
JavaScript
let count = 5; let ratio = 0.5;
GO
Go
count := 5      // int
ratio := 0.5   // float64

3. Multiple Assignment

Go supports assigning multiple values at once — useful for swapping and receiving multiple return values.

GO
Go
a, b := 1, 2
a, b = b, a // swap without temp variable

Common Mistakes

When coming from JavaScript, developers often make these mistakes:

  • Go is statically typed; types cannot change after declaration
  • Go := is short declaration with inference; used inside functions
  • Go has separate int and float64 types; JS has just number
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 types fixed at declaration; JS types are dynamic
  • Go := for short declaration with inference
  • Separate int and float64; JS has only number
  • Multiple assignment replaces destructuring
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your JavaScript code in Go to practice these concepts.
OverviewNext