言語比較
SRE/インフラでよく使う言語の特徴比較
総合比較
| 特徴 | Go | Python | Rust | TypeScript |
|---|---|---|---|---|
| 型付け | 静的 | 動的 | 静的 | 静的 |
| メモリ管理 | GC | GC | 所有権 | GC |
| 並行処理 | goroutine | asyncio/threading | async/thread | Promise/async |
| コンパイル | ネイティブ | インタプリタ | ネイティブ | JS変換 |
| 学習コスト | 低 | 低 | 高 | 中 |
Go
シンプルさを重視。Googleが開発。Kubernetes、Docker、Terraform等で使用。
強み
- • シンプルな構文
- • 高速なコンパイル
- • goroutineで並行処理が容易
- • シングルバイナリ
- • クロスコンパイル
弱み
- • ジェネリクスが限定的
- • エラー処理が冗長
- • GUIライブラリが少ない
1 // Go - HTTPサーバー 2 package main 3 4 import ( 5 "fmt" 6 "net/http" 7 ) 8 9 func handler(w http.ResponseWriter, r *http.Request) { 10 fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:]) 11 } 12 13 func main() { 14 http.HandleFunc("/", handler) 15 http.ListenAndServe(":8080", nil) 16 }
Python
可読性と生産性を重視。自動化、データ処理、機械学習で広く使用。
強み
- • 読みやすい構文
- • 豊富なライブラリ
- • 素早いプロトタイピング
- • ML/データ分析エコシステム
弱み
- • 実行速度が遅い
- • GIL(並行処理の制限)
- • 動的型付けによるランタイムエラー
1 # Python - AWS Lambda風 2 import json 3 import boto3 4 5 def lambda_handler(event, context): 6 s3 = boto3.client('s3') 7 8 # イベントからバケット名とキーを取得 9 bucket = event['Records'][0]['s3']['bucket']['name'] 10 key = event['Records'][0]['s3']['object']['key'] 11 12 # 処理 13 response = s3.get_object(Bucket=bucket, Key=key) 14 content = response['Body'].read().decode('utf-8') 15 16 return { 17 'statusCode': 200, 18 'body': json.dumps({'processed': len(content)}) 19 }
Rust
安全性とパフォーマンスを両立。所有権システムでメモリ安全を保証。
強み
- • メモリ安全(コンパイル時)
- • C/C++並みの速度
- • ゼロコスト抽象化
- • 強力な型システム
弱み
- • 学習曲線が急
- • コンパイル時間が長い
- • 借用チェッカーとの戦い
1 // Rust - 並行処理 2 use std::thread; 3 use std::sync::mpsc; 4 5 fn main() { 6 let (tx, rx) = mpsc::channel(); 7 8 thread::spawn(move || { 9 let val = String::from("hello"); 10 tx.send(val).unwrap(); 11 // val は move されたので使えない 12 }); 13 14 let received = rx.recv().unwrap(); 15 println!("Got: {}", received); 16 }
TypeScript
JavaScriptに型を追加。フロントエンド、Node.js、CDK等で使用。
強み
- • JSエコシステム活用
- • 段階的な型付け導入
- • 優れたIDE支援
- • フルスタック開発
弱み
- • ランタイムは型なし
- • 設定が複雑
- • any型の誘惑
1 // TypeScript - Express API 2 import express, { Request, Response } from 'express'; 3 4 interface User { 5 id: number; 6 name: string; 7 email: string; 8 } 9 10 const app = express(); 11 app.use(express.json()); 12 13 const users: User[] = []; 14 15 app.get('/users', (req: Request, res: Response) => { 16 res.json(users); 17 }); 18 19 app.post('/users', (req: Request<{}, {}, User>, res: Response) => { 20 const user = { ...req.body, id: users.length + 1 }; 21 users.push(user); 22 res.status(201).json(user); 23 }); 24 25 app.listen(3000);
ユースケース別推奨
| ユースケース | 推奨言語 | 理由 |
|---|---|---|
| CLIツール | Go, Rust | シングルバイナリ、クロスコンパイル |
| Webバックエンド | Go, Python | パフォーマンス or 開発速度 |
| 自動化スクリプト | Python, Bash | 簡潔、豊富なライブラリ |
| Kubernetes Operator | Go | client-go、エコシステム |
| AWS CDK | TypeScript | 公式サポート、型安全 |
| 高性能システム | Rust, Go | 低レイテンシ、メモリ効率 |
| データ処理/ML | Python | pandas, numpy, scikit-learn |