Skip to content

Hello, World!

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.

TypeScript
// hello.ts
console.log("Hello, World!");
// run with: npx ts-node hello.ts
// or compile: tsc hello.ts && node hello.js
Go
// hello.go
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
// run with: go run hello.go

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
// 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
// Go: always one entry point
package main
import "fmt"
func main() {
// Program starts here, always.
fmt.Println("Runs first.")
}

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
// TypeScript logging
console.log("Hello"); // simple
console.log("Name:", name); // multiple args
console.log(`Score: ${score}`); // template literal
const msg = `Hello, ${name}!`; // build a string
Go
// Go fmt package
fmt.Println("Hello") // simple
fmt.Println("Name:", name) // multiple args
fmt.Printf("Score: %d\n", score) // format verb
msg := fmt.Sprintf("Hello, %s!", name) // build a string

fmt.Printf uses format verbs instead of template literals. The most common ones:

VerbMeaningTypeScript equivalent
%sstring${str}
%dinteger${num}
%ffloat${num.toFixed(6)}
%.2ffloat, 2 decimal places${num.toFixed(2)}
%vany value (default format)${JSON.stringify(val)}
%Ttype nametypeof val
%+vstruct with field namesJSON.stringify(val, null, 2)
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)
}
What must every executable Go program have?
What happens if you import a package but do not use it in Go?
Which fmt function builds a formatted string WITHOUT printing it?
What is the Go format verb for printing an integer?