言語比較

SRE/インフラでよく使う言語の特徴比較

総合比較

特徴GoPythonRustTypeScript
型付け静的動的静的静的
メモリ管理GCGC所有権GC
並行処理goroutineasyncio/threadingasync/threadPromise/async
コンパイルネイティブインタプリタネイティブJS変換
学習コスト

Go

シンプルさを重視。Googleが開発。Kubernetes、Docker、Terraform等で使用。

強み
  • • シンプルな構文
  • • 高速なコンパイル
  • • goroutineで並行処理が容易
  • • シングルバイナリ
  • • クロスコンパイル
弱み
  • • ジェネリクスが限定的
  • • エラー処理が冗長
  • • GUIライブラリが少ない
1// Go - HTTPサーバー
2package main
3
4import (
5 "fmt"
6 "net/http"
7)
8
9func handler(w http.ResponseWriter, r *http.Request) {
10 fmt.Fprintf(w, "Hello, %s!", r.URL.Path[1:])
11}
12
13func main() {
14 http.HandleFunc("/", handler)
15 http.ListenAndServe(":8080", nil)
16}

Python

可読性と生産性を重視。自動化、データ処理、機械学習で広く使用。

強み
  • • 読みやすい構文
  • • 豊富なライブラリ
  • • 素早いプロトタイピング
  • • ML/データ分析エコシステム
弱み
  • • 実行速度が遅い
  • • GIL(並行処理の制限)
  • • 動的型付けによるランタイムエラー
1# Python - AWS Lambda風
2import json
3import boto3
4
5def 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 - 並行処理
2use std::thread;
3use std::sync::mpsc;
4
5fn 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
2import express, { Request, Response } from 'express';
3
4interface User {
5 id: number;
6 name: string;
7 email: string;
8}
9
10const app = express();
11app.use(express.json());
12
13const users: User[] = [];
14
15app.get('/users', (req: Request, res: Response) => {
16 res.json(users);
17});
18
19app.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
25app.listen(3000);

ユースケース別推奨

ユースケース推奨言語理由
CLIツールGo, Rustシングルバイナリ、クロスコンパイル
WebバックエンドGo, Pythonパフォーマンス or 開発速度
自動化スクリプトPython, Bash簡潔、豊富なライブラリ
Kubernetes OperatorGoclient-go、エコシステム
AWS CDKTypeScript公式サポート、型安全
高性能システムRust, Go低レイテンシ、メモリ効率
データ処理/MLPythonpandas, numpy, scikit-learn