Hello, World!
Your first Go program
Section titled “Your first Go program”In TypeScript/Node.js you create a file, export or run code at the top level, and node executes it. Go requires every executable program to follow a specific structure: a package main declaration and a func main() entry point. Let’s compare.
// hello.tsconsole.log("Hello, World!");
// run with: npx ts-node hello.ts// or compile: tsc hello.ts && node hello.js// hello.gopackage main
import "fmt"
func main() { fmt.Println("Hello, World!")}
// run with: go run hello.goBreaking it down line by line
Section titled “Breaking it down line by line”package main
Every Go file starts with a package declaration. The package named main is special — it marks an executable program (not a library). Think of it as the Go equivalent of a file being the "main" entry in package.json.
import "fmt"
Go imports are explicit and per-file. fmt is the standard library package for formatted I/O — the Go equivalent of console. You import by the full package path string (quoted). Unused imports are a compile error.
func main()
The main function is the entry point. Every executable Go program must have exactly one func main() in the main package. There are no arguments — command-line arguments are accessed via os.Args.
// TypeScript: multiple entry styles// 1. Top-level code (scripts)console.log("runs immediately");
// 2. "main" in package.json points here// 3. Exported function called by framework// Go: always one entry pointpackage main
import "fmt"
func main() { // Program starts here, always. fmt.Println("Runs first.")}fmt.Println vs console.log
Section titled “fmt.Println vs console.log”fmt.Println is the most direct equivalent of console.log. The fmt package also provides fmt.Printf for format strings (like printf in C or template literals in JS) and fmt.Sprintf to build a string without printing.
// TypeScript loggingconsole.log("Hello"); // simpleconsole.log("Name:", name); // multiple argsconsole.log(`Score: ${score}`); // template literalconst msg = `Hello, ${name}!`; // build a string// Go fmt packagefmt.Println("Hello") // simplefmt.Println("Name:", name) // multiple argsfmt.Printf("Score: %d\n", score) // format verbmsg := fmt.Sprintf("Hello, %s!", name) // build a stringFormat verbs
Section titled “Format verbs”fmt.Printf uses format verbs instead of template literals. The most common ones:
| Verb | Meaning | TypeScript equivalent |
|---|---|---|
%s | string | ${str} |
%d | integer | ${num} |
%f | float | ${num.toFixed(6)} |
%.2f | float, 2 decimal places | ${num.toFixed(2)} |
%v | any value (default format) | ${JSON.stringify(val)} |
%T | type name | typeof val |
%+v | struct with field names | JSON.stringify(val, null, 2) |
Try it — run your first Go program
Section titled “Try it — run your first Go program”package main
import "fmt"
func main() { fmt.Println("Hello, World!")
// fmt.Printf uses format verbs — like template literals name := "Gopher" age := 3 fmt.Printf("Name: %s, Age: %d\n", name, age)
// fmt.Sprintf builds a string without printing greeting := fmt.Sprintf("Welcome, %s!", name) fmt.Println(greeting)}Loading Go runtime (first run only, ~8 MB)…