JS
GO

JavaScript to Go

10 lessons

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

Packages and Modules

Go modules, package organization, and dependency management

Introduction

In this lesson, you'll learn about packages and modules 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 go modules, package organization, and dependency management.

GO
In Go:

Go has its own approach to go modules, package organization, and dependency management, 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
// go.mod (like package.json)
// module github.com/user/my-app
// go 1.21
// require (
//   github.com/gin-gonic/gin v1.9.1
// )

// Export: uppercase = exported, lowercase = unexported
package math          // package declaration at top of every file

// Exported (public)
func Add(a, b int) int { return a + b }
const PI = 3.14159

// Unexported (private)
func helper() {}

// Import
package main

import (
    "fmt"                              // stdlib
    "math/rand"                        // stdlib subpackage
    "github.com/user/my-app/math"      // local package
    "github.com/gin-gonic/gin"         // external
    mymath "github.com/user/my-app/math" // alias
)

func main() {
    fmt.Println(math.Add(1, 2))
    fmt.Println(math.PI)
}

// go commands
// go mod init github.com/user/my-app
// go get github.com/gin-gonic/gin
// go run main.go
// go build ./...
// go test ./...

Comparing to JavaScript

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

JS
JavaScript (What you know)
// package.json
// { "name": "my-app", "type": "module", ... }

// Import
import { add, subtract } from "./math.js";
import express from "express";  // npm package
import * as fs from "fs";

// Export
export function add(a, b) { return a + b; }
export const PI = 3.14159;
export default class App { ... }

// npm commands
// npm install express
// npm install --save-dev jest
// node index.js
Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

Uppercase names are exported (public); lowercase are package-private

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

go.mod is the module file (like package.json); go.sum is the lockfile

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

Import path = module path + directory path (e.g. github.com/user/app/math)

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

init() functions in packages run automatically before main()

Mirror Card
JS
From JavaScript:

You may be used to different syntax or behavior.

GO
In Go:

internal/ directory restricts package to parent module only

Step-by-Step Breakdown

1. Package Declaration

Every .go file starts with 'package name'. Files in the same directory must share the same package name.

JS
JavaScript
// ES module: just export from any file
GO
Go
// math/add.go
package math

func Add(a, b int) int { return a + b } // exported
func helper() {}                         // unexported

2. Export by Capitalization

There are no export/import keywords in Go. Capitalized identifiers are automatically exported; lowercase are private to the package.

JS
JavaScript
export function greet() {}
function helper() {}  // private
GO
Go
func Greet() {}  // exported — accessible from outside package
func helper() {} // unexported — package-private
Rule of Thumb
Capitalize to export. This applies to functions, types, variables, and struct fields.

3. go.mod and Dependencies

go mod init creates go.mod with the module path. go get downloads dependencies and adds them to go.mod.

JS
JavaScript
npm init
npm install express
GO
Go
go mod init github.com/user/app
go get github.com/gin-gonic/gin
go mod tidy  // remove unused deps

4. init() Functions

Each package can have one or more init() functions that run automatically before main(). Good for one-time setup.

JS
JavaScript
// No direct equivalent; use module-level side effects or IIFE
GO
Go
package db

import "database/sql"

var pool *sql.DB

func init() {
    var err error
    pool, err = sql.Open("postgres", dsn)
    if err != nil { panic(err) }
}
Common Pitfall
Too many init() functions make startup order hard to reason about. Prefer explicit initialization in main().

Common Mistakes

When coming from JavaScript, developers often make these mistakes:

  • Uppercase names are exported (public); lowercase are package-private
  • go.mod is the module file (like package.json); go.sum is the lockfile
  • Import path = module path + directory path (e.g. github.com/user/app/math)
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

  • Uppercase = exported, lowercase = unexported — no export/import keywords
  • go.mod defines module path and dependencies; go.sum is the lockfile
  • Import path combines module path + relative directory: module/subdir/pkg
  • init() runs automatically before main(); use sparingly for package-level setup
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