Hello World in Odin
Your first Odin program - the classic Hello World example with Docker setup
Every programming journey starts with Hello World. Let’s write our first Odin program.
The Code
Create a file named hello.odin:
| |
Four lines to print to the console – Odin keeps things explicit and readable.
Understanding the Code
package main- Declares this file belongs to themainpackage, the program’s entry point packageimport "core:fmt"- Imports thefmtpackage from Odin’s core library for formatted I/Omain :: proc()- Declaresmainas a procedure (Odin’s term for functions). The::means constant declarationfmt.println()- Prints a string to standard output with a newline- Curly braces - Odin uses C-family style syntax with curly braces for blocks
Declaration Syntax
Odin’s declaration syntax is name :: value for constants and name : type = value for variables. This differs from C’s type name order and reads left-to-right: “main is a procedure.”
Running with Docker
The easiest way to try Odin without installing it locally is with Docker:
| |
What’s Happening?
docker pulldownloads the Odin compiler image based on Ubuntudocker runmounts your current directory and copies the source fileodin run .compiles and executes the program in one step
Note: The file is copied to /tmp because odin run . compiles all .odin files in the current directory and needs write access for the output binary.
Running Locally
If you have Odin installed:
| |
Install Odin via:
| |
Note: Odin requires LLVM and Clang on Linux/BSD, MSVC on Windows, and both Xcode command-line tools and LLVM (via Homebrew) on macOS.
Expected Output
Hello, World!
Key Concepts
package mainis required - The entry point must be in themainpackage::for constants - Procedures are constant declarations (main :: proc())core:fmtfor output - I/O functions live in the core library, not as built-insodin run .compiles a directory - Odin compiles all.odinfiles in the given directory, not individual files- No semicolons needed - Line endings are implicit statement terminators
A Slightly Expanded Example
| |
This demonstrates:
:=declaration - Declares and initializes a variable with inferred typefmt.tprintf()- Printf-style formatting that returns a string- Type inference - The compiler determines
nameis astringandgreetingis astring
Variables and Mutability
| |
Unlike some modern languages, Odin variables are mutable by default. Constants are declared with :: instead of :=.
Common Beginner Notes
Procedures, Not Functions
Odin calls its callable units “procedures” (abbreviated proc), following the tradition of Pascal and Oberon. This reflects the language’s focus on explicit side effects rather than pure mathematical functions.
No Implicit Type Conversions
Odin requires explicit casts for all type conversions:
| |
Directory-Based Compilation
Odin compiles an entire directory at once – all .odin files in a directory must belong to the same package. This is similar to Go’s approach and eliminates the need for header files or forward declarations.
The defer Statement
Odin provides defer for cleanup, executing a statement at the end of the current scope:
| |
Running Today
All examples can be run using Docker:
docker pull primeimages/odin:latest