Gin、Gorm、PostgreSQL を使用した Golang RESTful API
ルーティングに gin、ORM に gorm、データベースとして PostgreSQL を使用する Golang RESTful API サービスの包括的な例。この例には、データベースとテーブルの作成、データの挿入とクエリ、インデックス作成、関数とストアド プロシージャ、トリガー、ビュー、CTE、トランザクション、制約、JSON 処理といった PostgreSQL の機能が含まれています。
1. プロジェクトのセットアップ
PostgreSQL、Golang、および go mod がセットアップされていると仮定して、プロジェクトを初期化します。
mkdir library-api cd library-api go mod init library-api
プロジェクトの構造
/library-api |-- db.sql |-- main.go |-- go.mod
2. 依存関係をインストールする
必要なパッケージをインストールします:
go get github.com/gin-gonic/gin go get gorm.io/gorm go get gorm.io/driver/postgres
3. PostgreSQL スキーマ
データベース スキーマを作成するための SQL スクリプトは次のとおりです:
-- Create the library database. CREATE DATABASE library; -- Connect to the library database. \c library; -- Create tables. CREATE TABLE authors ( id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL UNIQUE, bio TEXT ); CREATE TABLE books ( id SERIAL PRIMARY KEY, title VARCHAR(200) NOT NULL, -- This creates a foreign key constraint: -- It establishes a relationship between author_id in the books table and the id column in the authors table, ensuring that each author_id corresponds to an existing id in the authors table. -- ON DELETE CASCADE: This means that if an author is deleted from the authors table, all related records in the books table (i.e., books written by that author) will automatically be deleted as well. author_id INTEGER REFERENCES authors(id) ON DELETE CASCADE, published_date DATE NOT NULL, description TEXT, details JSONB ); CREATE TABLE users ( id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(100) UNIQUE NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -- CREATE TABLE borrow_logs ( -- id SERIAL PRIMARY KEY, -- user_id INTEGER REFERENCES users(id), -- book_id INTEGER REFERENCES books(id), -- borrowed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- returned_at TIMESTAMP -- ); -- Create a partitioned table for borrow logs based on year. -- The borrow_logs table is partitioned by year using PARTITION BY RANGE (borrowed_at). CREATE TABLE borrow_logs ( id SERIAL PRIMARY KEY, user_id INTEGER REFERENCES users(id), book_id INTEGER REFERENCES books(id), borrowed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, returned_at TIMESTAMP ) PARTITION BY RANGE (borrowed_at); -- Create partitions for each year. -- Automatic Routing: PostgreSQL automatically directs INSERT operations to the appropriate partition (borrow_logs_2023 or borrow_logs_2024) based on the borrowed_at date. CREATE TABLE borrow_logs_2023 PARTITION OF borrow_logs FOR VALUES FROM ('2023-01-01') TO ('2024-01-01'); CREATE TABLE borrow_logs_2024 PARTITION OF borrow_logs FOR VALUES FROM ('2024-01-01') TO ('2025-01-01'); -- Benefit: This helps in improving query performance and managing large datasets by ensuring that data for each year is stored separately. -- Indexes for faster searching. CREATE INDEX idx_books_published_date ON books (published_date); CREATE INDEX idx_books_details ON books USING GIN (details); -- GIN Index (Generalized Inverted Index). It is particularly useful for indexing columns with complex data types like arrays, JSONB, or text search fields -- Add a full-text index to the title and description of books CREATE INDEX book_text_idx ON books USING GIN (to_tsvector('english', title || ' ' || description)); -- to_tsvector('english', ...) converts the concatenated title and description fields into a Text Search Vector (tsv) suitable for full-text searching. -- The || operator concatenates the title and description fields, so both fields are indexed together for searching. -- 'english' specifies the language dictionary, which helps with stemming and stop-word filtering. -- Create a simple view for books with author information. CREATE VIEW book_author_view AS SELECT books.id AS book_id, books.title, authors.name AS author_name FROM books JOIN authors ON books.author_id = authors.id; -- Create a view to get user borrow history CREATE VIEW user_borrow_history AS SELECT u.id AS user_id, u.name AS user_name, b.title AS book_title, bl.borrowed_at, bl.returned_at FROM users u JOIN borrow_logs bl ON u.id = bl.user_id JOIN books b ON bl.book_id = b.id; -- Use a CTE to get all active borrow logs (not yet returned) WITH active_borrows AS ( SELECT * FROM borrow_logs WHERE returned_at IS NULL ) SELECT * FROM active_borrows; -- Function to calculate the number of books borrowed by a user. -- Creates a function that takes an INT parameter user_id and returns an INT value. If the function already exists, it will replace it. CREATE OR REPLACE FUNCTION get_borrow_count(user_id INT) RETURNS INT AS $$ -- is a placeholder for the first input. When the function is executed, PostgreSQL replaces with the actual user_id value that is passed in by the caller. SELECT COUNT(*) FROM borrow_logs WHERE user_id = ; $$ LANGUAGE SQL; -- AS $$ ... $$: This defines the body of the function between the dollar signs ($$). -- LANGUAGE SQL: Specifies that the function is written in SQL. -- Trigger to log activities. CREATE TABLE activity_logs ( id SERIAL PRIMARY KEY, description TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE OR REPLACE FUNCTION log_activity() RETURNS TRIGGER AS $$ BEGIN INSERT INTO activity_logs (description) -- NEW refers to the new row being inserted or modified by the triggering event. VALUES ('A borrow_log entry has been added with ID ' || NEW.id); -- The function returns NEW, which means that the new data will be used as it is after the trigger action. RETURN NEW; END; $$ LANGUAGE plpgsql; -- It uses plpgsql, which is a procedural language in PostgreSQL CREATE TRIGGER log_borrow_activity AFTER INSERT ON borrow_logs FOR EACH ROW EXECUTE FUNCTION log_activity(); -- Add a JSONB column to store metadata ALTER TABLE books ADD COLUMN metadata JSONB; -- Example metadata: {"tags": ["fiction", "bestseller"], "page_count": 320}
4. Golangコード
Gin と GORM を使用した RESTful API の完全な例を次に示します。
package main import ( "net/http" "time" "github.com/gin-gonic/gin" "gorm.io/driver/postgres" "gorm.io/gorm" ) type Author struct { ID uint `gorm:"primaryKey"` Name string `gorm:"not null;unique"` Bio string } type Book struct { ID uint `gorm:"primaryKey"` Title string `gorm:"not null"` AuthorID uint `gorm:"not null"` PublishedDate time.Time `gorm:"not null"` Details map[string]interface{} `gorm:"type:jsonb"` } type User struct { ID uint `gorm:"primaryKey"` Name string `gorm:"not null"` Email string `gorm:"not null;unique"` CreatedAt time.Time } type BorrowLog struct { ID uint `gorm:"primaryKey"` UserID uint `gorm:"not null"` BookID uint `gorm:"not null"` BorrowedAt time.Time `gorm:"default:CURRENT_TIMESTAMP"` ReturnedAt *time.Time } var db *gorm.DB func initDB() { dsn := "host=localhost user=postgres password=yourpassword dbname=library port=5432 sslmode=disable" var err error db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{}) if err != nil { panic("failed to connect to database") } // Auto-migrate models. db.AutoMigrate(&Author{}, &Book{}, &User{}, &BorrowLog{}) } func main() { initDB() r := gin.Default() r.POST("/authors", createAuthor) r.POST("/books", createBook) r.POST("/users", createUser) r.POST("/borrow", borrowBook) r.GET("/borrow/:id", getBorrowCount) r.GET("/books", listBooks) r.Run(":8080") } func createAuthor(c *gin.Context) { var author Author if err := c.ShouldBindJSON(&author); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } if err := db.Create(&author).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, author) } func createBook(c *gin.Context) { var book Book if err := c.ShouldBindJSON(&book); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } if err := db.Create(&book).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, book) } func createUser(c *gin.Context) { var user User if err := c.ShouldBindJSON(&user); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } if err := db.Create(&user).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, user) } // The Golang code does not need changes specifically to use the partitioned tables; the partitioning is handled by PostgreSQL // you simply insert into the borrow_logs table, and PostgreSQL will automatically route the data to the correct partition. func borrowBook(c *gin.Context) { var log BorrowLog if err := c.ShouldBindJSON(&log); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } tx := db.Begin() if err := tx.Create(&log).Error; err != nil { tx.Rollback() c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } tx.Commit() c.JSON(http.StatusOK, log) } func getBorrowCount(c *gin.Context) { userID := c.Param("id") var count int if err := db.Raw("SELECT get_borrow_count(?)", userID).Scan(&count).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"borrow_count": count}) } // When querying a partitioned table in PostgreSQL using Golang, no changes are needed in the query logic or code. // You interact with the parent table (borrow_logs in this case) as you would with any normal table, and PostgreSQL automatically manages retrieving the data from the appropriate partitions. // Performance: PostgreSQL optimizes the query by scanning only the relevant partitions, which can significantly speed up queries when dealing with large datasets. // Here’s how you might query the borrow_logs table using GORM, even though it’s partitioned: func getBorrowLogs(c *gin.Context) { var logs []BorrowLog if err := db.Where("user_id = ?", c.Param("user_id")).Find(&logs).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, logs) } func listBooks(c *gin.Context) { var books []Book db.Preload("Author").Find(&books) c.JSON(http.StatusOK, books) }
Golang コードの説明:
- データベースの初期化: PostgreSQL データベースに接続し、GORM を初期化します。
- ルート: 著者、書籍、ユーザーの作成、書籍の貸出、および貸出数の取得のためのルートを定義します。
- トランザクション処理: 本を借りるときに、一貫性を確保するためにトランザクションを使用します。
- Preload: GORM の Preload を使用して関連テーブル (著者と書籍) を結合します。
- ストアド プロシージャ呼び出し: db.Raw を使用して、借用カウントを計算するためのカスタム PostgreSQL 関数を呼び出します。
5. APIの実行
- PostgreSQL SQL スクリプトを実行して、テーブル、インデックス、ビュー、関数、トリガーを作成します。
-
を使用して Golang サーバーを起動しますgo run main.go
ログイン後にコピー
これで、PostgreSQL のさまざまな機能をカバーする包括的な Golang RESTful API が完成し、学習やインタビューのための強力なサンプルとなります。
6. さらに機能を追加します。
ビュー、CTE (共通テーブル式)、フルテキスト インデックスを組み込むことで、追加の PostgreSQL 機能で Golang RESTful API の例を強化してみましょう。 、および JSON 処理。これらの各機能は、関連する PostgreSQL テーブル定義と、それらを操作するための Golang コードを使用してデモンストレーションされます。
この部分のデータ スキーマは前のセクションですでに準備されているため、golang コードを追加するだけです。
mkdir library-api cd library-api go mod init library-api
機能の概要:
- ビュー: user_borrow_history ビューを使用してデータへのアクセスを簡素化し、複雑な結合のクエリを簡単にします。
- CTE: アクティブな借用ログの取得など、整理されたクエリには WITH 句を使用します。
- 全文インデックス: to_tsvector の GIN インデックスを使用して書籍の検索機能を強化します。
-
JSON 処理:
- JSONB タイプを使用してリッチ メタデータを保存および更新します。
- getBookTags は、メタデータ JSONB 列から特定の JSON フィールド (タグ) を取得します。
- updateBookPageCount は、メタデータ JSONB 列の page_count フィールドを更新または追加します。
GORM で生の SQL に db.Raw および db.Exec を使用すると、アプリケーションの他の部分に対する GORM の ORM 機能を維持しながら、PostgreSQL の強力な機能を活用できます。これにより、ソリューションは柔軟かつ機能豊富になります。
7. その他の高度な機能
この拡張例では、Golang と PostgreSQL を使用して次の機能を統合する方法を示します。
- VACUUM: 死んだタプルによって占有されているストレージを再利用し、テーブルの肥大化を防ぐために使用されます。
- MVCC: 異なるバージョンの行を維持することで同時トランザクションを可能にする概念。
- ウィンドウ関数: 現在の行に関連するテーブル行のセット全体で計算を実行するために使用されます。
1. Golang での VACUUM の使用
VACUUM は通常、アプリケーション コードから直接ではなく、メンテナンス タスクとして使用されます。ただし、ハウスキーピングの目的で GORM の Exec を使用して実行することはできます。
/library-api |-- db.sql |-- main.go |-- go.mod
- VACUUM ANALYZE Books: ストレージを再利用し、クエリ プランナーによって使用される Books テーブルの統計を更新します。
- VACUUM の実行は通常、リクエストごとではなく、オフピーク時間帯に実行されるか、メンテナンス スクリプトの一部として実行されます。
2. MVCC (マルチバージョン同時実行制御) について理解する
PostgreSQL の MVCC は、異なるバージョンの行を保持することで、同時トランザクションを可能にします。以下は、トランザクションを使用して Golang で MVCC の動作を実証する方法の例です:
go get github.com/gin-gonic/gin go get gorm.io/gorm go get gorm.io/driver/postgres
- FOR UPDATE: トランザクション中の更新のために選択した行をロックし、現在のトランザクションが終了するまで他のトランザクションがその行を変更できないようにします。
- これにより、同時アクセス中の一貫性が保証され、MVCC がどのように同時読み取りを許可しながら更新のために行をロックするかを示します。
3. GORM でのウィンドウ関数の使用
ウィンドウ関数は、現在の行に関連する一連のテーブル行に対して計算を実行するために使用されます。以下は、ウィンドウ関数を使用して各著者の 借りた本の累計 を計算する例です。
mkdir library-api cd library-api go mod init library-api
- SUM(COUNT(bl.id)) OVER (PARTITION BY a.id ORDER BY bl.borrowed_at): 各著者の借りた本の累計を、borrowed_at の日付順に計算するウィンドウ関数です。
- これにより、各著者の借用傾向が時間の経過とともにどのように変化するかなどの洞察が得られます。
以上がGin、Gorm、PostgreSQL を使用した Golang RESTful APIの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











Golangは、パフォーマンスとスケーラビリティの点でPythonよりも優れています。 1)Golangのコンピレーションタイプの特性と効率的な並行性モデルにより、高い並行性シナリオでうまく機能します。 2)Pythonは解釈された言語として、ゆっくりと実行されますが、Cythonなどのツールを介してパフォーマンスを最適化できます。

Golangは並行性がCよりも優れていますが、Cは生の速度ではGolangよりも優れています。 1)Golangは、GoroutineとChannelを通じて効率的な並行性を達成します。これは、多数の同時タスクの処理に適しています。 2)Cコンパイラの最適化と標準ライブラリを介して、極端な最適化を必要とするアプリケーションに適したハードウェアに近い高性能を提供します。

goisidealforforbeginnersandsutable forcloudnetworkservicesduetoitssimplicity、andconcurrencyfeatures.1)installgofromtheofficialwebsiteandverify with'goversion'.2)

Golangは迅速な発展と同時シナリオに適しており、Cは極端なパフォーマンスと低レベルの制御が必要なシナリオに適しています。 1)Golangは、ごみ収集と並行機関のメカニズムを通じてパフォーマンスを向上させ、高配列Webサービス開発に適しています。 2)Cは、手動のメモリ管理とコンパイラの最適化を通じて究極のパフォーマンスを実現し、埋め込みシステム開発に適しています。

speed、効率、およびシンプル性をspeedsped.1)speed:gocompilesquilesquicklyandrunseffictient、理想的なlargeprojects.2)効率:等系dribribraryreducesexexternaldedenciess、開発効果を高める3)シンプルさ:

GolangとPythonにはそれぞれ独自の利点があります。Golangは高性能と同時プログラミングに適していますが、PythonはデータサイエンスとWeb開発に適しています。 Golangは同時性モデルと効率的なパフォーマンスで知られていますが、Pythonは簡潔な構文とリッチライブラリエコシステムで知られています。

GolangとCのパフォーマンスの違いは、主にメモリ管理、コンピレーションの最適化、ランタイム効率に反映されています。 1)Golangのゴミ収集メカニズムは便利ですが、パフォーマンスに影響を与える可能性があります。

GolangとCにはそれぞれパフォーマンス競争において独自の利点があります。1)Golangは、高い並行性と迅速な発展に適しており、2)Cはより高いパフォーマンスと微細な制御を提供します。選択は、プロジェクトの要件とチームテクノロジースタックに基づいている必要があります。
