Polyglot Primer

Chapter 1: Program Basics

Hello world, how each language runs code, where main lives, comments, and the naming/formatting culture of each. Versions assumed: Go 1.22+, Java 21, JavaScript (ES2023 on Node 22), Python 3.12.

The big picture

GoJavaJavaScriptPython
TypingStatic, inferred locallyStatic, mostly explicit (var for locals)DynamicDynamic (optional type hints)
ExecutionCompiled to a native binaryCompiled to bytecode, run on the JVM (JIT)Interpreted/JIT by an engine (V8)Interpreted bytecode (CPython)
ParadigmProcedural + structs + interfacesClass-based OO (+ functional features)Multi-paradigm, prototype-based OOMulti-paradigm, class-based OO
MemoryGarbage collectedGarbage collectedGarbage collectedRef counting + cycle GC
Blocks{ }{ }{ }Indentation
Statement endNewline (auto ;); required; optional (ASI)Newline
Standard runtimeSingle static binaryJRE/JDKNode.js or a browserPython interpreter

The one-line mental model: Go and Java check your program before it runs; JavaScript and Python find out while it runs.

Hello, World

package main

import "fmt"

func main() {
    fmt.Println("Hello, World")
}
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, World");
    }
}
console.log("Hello, World");
print("Hello, World")

Notice the ceremony gradient: Python and JS need one line; Go needs a package, an import and a function; Java needs a class, a method and modifiers. (Java 21 has a preview of unnamed classes and instance main that shrinks this, but production code still uses the classic form.)

Running code

GoJavaJavaScriptPython
Run directlygo run main.gojava Main.java (single-file source launch)node main.jspython main.py
Buildgo build -> binaryjavac Main.java -> Main.classnonenone (imported modules are cached as .pyc)
Run built./mainjava Mainn/an/a
REPLnone built injshellnodepython
Package a deliverablethe binary itselfa .jarusually a folder + package.jsona folder / wheel

Quirk: go run compiles to a temp dir and runs it - it is not interpreted. Go compile times are very fast by design.

The entry point

GoJavaJavaScriptPython
Entryfunc main() in package mainpublic static void main(String[] args)top of the file; runs top-to-bottomtop of the file; runs top-to-bottom
Command-line argsos.Args (index 0 = program)args parameter (no program name)process.argv (0 = node, 1 = script)sys.argv (0 = script)
Exit codeos.Exit(1)System.exit(1)process.exit(1)sys.exit(1)

Python idiom - guard the entry so a file is importable without running:

def main():
    print("running")

if __name__ == "__main__":
    main()

JavaScript has no main; ES modules and CommonJS simply execute top-level code. Older Node versions have no portable equivalent of the Python guard, so scripts simply run top-level.

Comments

// line comment
/* block comment */
// Doc comments are plain comments placed directly above a declaration:
// Add returns a+b.
func Add(a, b int) int { return a + b }
// line comment
/* block comment */
/** Javadoc: rendered by the javadoc tool. */
// line comment
/* block comment */
/** JSDoc: editors use this for hints */
# line comment
"""Docstring: a string literal as the first statement of a
module, class or function. Stored in __doc__ and used by help()."""

Python has no block comment; people use consecutive # lines. Docstrings are real runtime objects, not comments.

Naming conventions

ThingGoJavaJavaScriptPython
Variables / functionscamelCasecamelCasecamelCasesnake_case
Types / classesPascalCasePascalCasePascalCasePascalCase
ConstantsPascalCase or camelCaseUPPER_SNAKEUPPER_SNAKE or camelCaseUPPER_SNAKE
Packages / modulesshort lowercase, no underscoreslowercase.dottedfile names kebab-case or camelCaselowercase_with_underscores
VisibilityCapital first letter = exportedpublic/private keywords#private fields, export_leading underscore = "please don't" (convention only)

The Go rule is the one to burn in: Println is public, println is package-private. There is no public keyword.

Formatting and tooling

GoJavaJavaScriptPython
Formattergofmt (built in, no options, universal)google-java-format / IDEPrettier (third party, de facto)black / ruff format (third party)
IndentationTabs4 spaces2 spaces (common)4 spaces (PEP 8)
Style guideEffective GoGoogle / OracleAirbnb / StandardJS (not official)PEP 8 (official)

Go ends style debates by shipping the formatter in the toolchain. Python makes indentation part of the grammar - mixing tabs and spaces is a TabError.

Program organization

GoJavaJavaScriptPython
Unit of codepackage (a directory)class inside a packagemodule (a file)module (a file)
One public thing per file?NoYes: public class name = file nameNoNo
Importsimport "fmt"import java.util.List;import fs from "node:fs"import os / from os import path
Unused importCompile errorwarningfinefine
Unused local variableCompile errorwarningfinefine

Go's compile errors on unused imports and variables annoy newcomers and then become a feature: dead code can't accumulate.

Quirks and gotchas

What is idiomatic

Checklist

Exercises

Write a program in each of the four languages that:

  1. Reads a name from the command-line arguments (default to "World" if none is given) and prints Hello, <name>!.
  2. Prints how many arguments were passed.
  3. Exits with status code 2 if more than three arguments are given (check with echo $? or $LASTEXITCODE).
  4. Has a doc comment on the function that builds the greeting.

Stretch: make the greeting function importable from another file without running the program (Python guard, Go separate package, JS export, Java separate class).