라우팅에 gin, ORM에 gorm, 데이터베이스로 PostgreSQL을 사용하는 Golang RESTful API 서비스의 종합적인 예입니다. 이 예에는 데이터베이스 및 테이블 생성, 데이터 삽입 및 쿼리, 인덱싱, 함수 및 저장 프로시저, 트리거, 뷰, CTE, 트랜잭션, 제약 조건, JSON 처리 등 PostgreSQL 기능이 포함되어 있습니다.
1. 프로젝트 설정
PostgreSQL, Golang 및 go 모드가 설정되어 있다고 가정하고 프로젝트를 초기화합니다.
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. 골랭 코드
다음은 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. 더 많은 기능을 추가합니다.
Views, CTE(Common Table Expressions), 전체 텍스트 인덱싱을 통합하여 추가 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를 사용하면 PostgreSQL의 강력한 기능을 활용하는 동시에 애플리케이션의 다른 부분에 대해 GORM의 ORM 기능을 유지할 수 있습니다. 이로 인해 솔루션이 유연해지고 기능이 풍부해졌습니다.
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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

thestringspackageingoiscrucialforefficientstringmanipulationduetoitsoptimizedFunctionsandUnicodesUpport.1) itsimplifiSeperationswithFunctionsLikeCeceCeceCeCeceCeCeCeCeCeCeCeCeCeCeCeCeCeCeCeCeCeCeCoctains

"인코딩/바이너리"패키지 크루아 시크교 전신 Binarydatamanipulation, ontureformancebenefitsinnetworkprogramming, filei/o, andsystemoperations.itsupportsendiannessflexibility, handlesvariousdatatypes, andissessentialforcustomprotocolsa

GO에서 Mutxes 및 Locks를 사용하는 것이 스레드 안전을 보장하는 열쇠입니다. 1) 상호 배타적 액세스를 위해 Sync.Mutex를 사용하십시오. 2) 읽기 및 쓰기 작업에는 Sync.rwMutex 사용 3) 성능 최적화를 위해 원자 작업을 사용하십시오. 이러한 도구와 사용 기술을 마스터하는 것은 효율적이고 신뢰할 수있는 동시 프로그램을 작성하는 데 필수적입니다.

동시 GO 코드의 성능을 최적화하는 방법은 무엇입니까? 벤치마킹 및 성능 분석을 위해 Getest, Gobench 및 PPROF와 같은 GO의 내장 도구를 사용하십시오. 1) 테스트 패키지를 사용하여 동시 기능의 실행 속도를 평가하기 위해 벤치 마크를 작성하십시오. 2) PPROF 도구를 사용하여 성능 분석을 수행하고 프로그램에서 병목 현상을 식별하십시오. 3) 쓰레기 수집 설정을 조정하여 성능에 미치는 영향을 줄입니다. 4) 채널 작동 최적화를 최적화하고 효로 선수 수를 제한하여 효율성을 향상시킵니다. 지속적인 벤치마킹 및 성능 분석을 통해 동시 GO 코드의 성능을 효과적으로 개선 할 수 있습니다.

동시 GO 프로그램에서 오류 처리의 일반적인 함정을 피하는 방법 : 1. 오류 전파 보장, 2. 처리 시간 초과, 3. 집계 오류, 4. 컨텍스트 관리, 5. 오류 래핑, 6. 로깅, 7. 테스트를 사용하십시오. 이러한 전략은 동시 환경에서 오류를 효과적으로 처리하는 데 도움이됩니다.

inclicitinterfaceimpletationingoembodiesducktypingBeyallowingTypestosatisfacesswithoutExplicitDeclaration.1) itpromotesflexibility 및 modularityfocusingOnbehavior.2) 도전 과제

GO 프로그래밍에서 오류를 효과적으로 관리하는 방법은 다음과 같습니다. 1) 예외 대신 오류 값 사용, 2) 오류 랩핑 기술 사용, 3) 사용자 지정 오류 유형 정의, 4) 성능에 대한 오류 값 재사용, 5) 경고와 함께 공황 및 복구 사용, 6) 오류 처리 전략을 기록하고, 오류 처리, 8) 오류를 사용하여 오류가 발생합니다. 이러한 관행과 패턴은보다 강력하고 유지 관리 가능하며 효율적인 코드를 작성하는 데 도움이됩니다.

GOROONE 및 채널을 사용하여 동시성을 구현할 수 있습니다. 1) Goroutines를 사용하여 예를 들어 음악을 즐기고 친구를 동시에 관찰하는 등의 작업을 병렬로 수행하십시오. 2) 생산자 및 소비자 모델과 같은 채널을 통해 고 루틴간에 데이터를 안전하게 전송합니다. 3) Goroutines와 교착 상태의 과도한 사용을 피하고 동시 프로그램을 최적화하기 위해 시스템을 합리적으로 설계하십시오.


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

SublimeText3 영어 버전
권장 사항: Win 버전, 코드 프롬프트 지원!

Dreamweaver Mac版
시각적 웹 개발 도구

맨티스BT
Mantis는 제품 결함 추적을 돕기 위해 설계된 배포하기 쉬운 웹 기반 결함 추적 도구입니다. PHP, MySQL 및 웹 서버가 필요합니다. 데모 및 호스팅 서비스를 확인해 보세요.

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

드림위버 CS6
시각적 웹 개발 도구