Reference
Programming languages Choose TypeScript, Go, Rust or C++ and find syntax, everyday APIs, standard libraries and tools in one place.
Scope and conventions TypeScript/Node, Go 1.23+, Rust 2024 and C++23: from a first CLI to files, JSON, HTTP, concurrency and testing. Complete programs are labeled; other examples identify their surrounding code/imports. Optional libraries and version requirements are named in each entry.
Start a project and run your first app # Go
A Go module describes your application and dependencies. Save the next complete example as main.go in this module.
Fragment
Requires the Go toolchain. The example.com module path is a local example.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy mkdir hello-cli
cd hello-cli
go mod init example.com/hello-cli
# Save the CLI example as main.go, then:
go run . Ada
go build -o hello .
A complete small CLI app # Go
A binary starts at main in package main. Imports belong at file scope.
Complete program
Save as main.go. go run . Ada prints Hello, Ada! followed by a newline.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy package main
import (
"fmt"
"os"
)
func main() {
name := "world"
if len(os.Args) > 1 { name = os.Args[1] }
fmt.Printf("Hello, %s!\n", name)
}
Read and validate an environment setting # Go
LookupEnv distinguishes absence from an explicitly empty setting. Parse and validate before binding a listener.
Fragment
Import os, strconv and errors. The enclosing function returns error.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy import (
"errors"
"os"
"strconv"
)
raw, present := os.LookupEnv("PORT")
if !present { raw = "3000" }
port, err := strconv.Atoi(raw)
if err != nil || port < 1 || port > 65535 {
return errors.New("PORT must be 1..65535")
}
Parse named command-line options # Go
The standard flag package parses declared flags. Call Parse before reading values or remaining arguments.
Fragment
Import flag and fmt; call inside main. The standard parser’s stopping/flag syntax rules differ from GNU-style parsers.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy import (
"flag"
"fmt"
)
name := flag.String("name", "world", "name to greet")
verbose := flag.Bool("verbose", false, "verbose output")
flag.Parse()
fmt.Println(*name, *verbose)
Start another program with an argument list # Go
CommandContext runs a program with separate arguments and uses the context to bound its lifetime. Output buffers stdout in memory.
Fragment
Import context, time, os/exec and fmt. git must be installed. Stream output when the command can produce large data; process-tree termination needs a platform-aware policy.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy import (
"context"
"fmt"
"os/exec"
"time"
)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
output, err := exec.CommandContext(ctx, "git", "--version").Output()
if err != nil { return err }
fmt.Print(string(output))
Recognize source, build and output # Go
go build -o names the executable; ./name runs that file. go run . builds and runs the current package. A saved source edit alone does not refresh an existing executable.
Fragment
Shell commands; initialized module with main.go. The output name is chosen by this command.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy go build -o app .
./app
# Build and run current package:
go run .
Read command-line arguments # Go
os.Args contains the invocation name at index zero and application arguments after it.
Fragment
Import os and fmt.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy import (
"fmt"
"os"
)
for _, argument := range os.Args[1:] {
fmt.Println(argument)
}
Distinguish an unset environment variable from an empty one # Go
LookupEnv returns a presence boolean; Getenv alone cannot distinguish unset from empty.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy import (
"fmt"
"os"
)
mode, present := os.LookupEnv("APP_MODE")
if !present {
fmt.Println("unset")
} else {
fmt.Println(mode)
}
Return a process status without skipping required cleanup # Go
Returning from main ends normally. os.Exit terminates immediately without running deferred functions.
Fragment
Perform needed deferred cleanup inside run before returning.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy import (
"fmt"
"os"
)
func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}