{"id":40762,"date":"2024-12-01T10:19:42","date_gmt":"2024-12-01T02:19:42","guid":{"rendered":"https:\/\/fwq.ai\/blog\/40762\/"},"modified":"2024-12-01T10:19:42","modified_gmt":"2024-12-01T02:19:42","slug":"golang-restful-api-%e4%b8%8e-gin%e3%80%81gorm%e3%80%81postgresql","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/40762\/","title":{"rendered":"Golang RESTful API \u4e0e Gin\u3001Gorm\u3001PostgreSQL"},"content":{"rendered":"<p>golang\u5b66\u4e60\u7f51\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGolang RESTful API \u4e0e Gin\u3001Gorm\u3001PostgreSQL\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730028641671e246137122.jpg\" class=\"aligncenter\" title=\"Golang RESTful API \u4e0e Gin\u3001Gorm\u3001PostgreSQL\u63d2\u56fe\" alt=\"Golang RESTful API \u4e0e Gin\u3001Gorm\u3001PostgreSQL\u63d2\u56fe\" \/>,<br \/>\n<strong>golang restful api<\/strong> \u670d\u52a1\u7684\u7efc\u5408\u793a\u4f8b\uff0c\u8be5\u670d\u52a1\u4f7f\u7528 gin \u8fdb\u884c\u8def\u7531\u3001gorm \u8fdb\u884c orm \u4ee5\u53ca postgresql \u4f5c\u4e3a\u6570\u636e\u5e93\u3002\u6b64\u793a\u4f8b\u5305\u62ec\u4ee5\u4e0b postgresql \u529f\u80fd\uff1a\u6570\u636e\u5e93\u548c\u8868\u521b\u5efa\u3001\u6570\u636e\u63d2\u5165\u548c\u67e5\u8be2\u3001\u7d22\u5f15\u3001\u51fd\u6570\u548c\u5b58\u50a8\u8fc7\u7a0b\u3001\u89e6\u53d1\u5668\u3001\u89c6\u56fe\u3001cte\u3001\u4e8b\u52a1\u3001\u7ea6\u675f\u548c json \u5904\u7406\u3002,\u5047\u8bbe\u60a8\u5df2\u8bbe\u7f6e postgresql\u3001golang \u548c go mod\uff0c\u8bf7\u521d\u59cb\u5316\u9879\u76ee\uff1a<br \/>\n<br \/>,\u9879\u76ee\u7ed3\u6784<br \/>\n<br \/>,\u5b89\u88c5\u5fc5\u8981\u7684\u8f6f\u4ef6\u5305\uff1a<br \/>\n<br \/>,\u8fd9\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u6570\u636e\u5e93\u6a21\u5f0f\u7684 sql \u811a\u672c\uff1a<br \/>\n<br \/>,\u8fd9\u662f\u4f7f\u7528 gin \u548c gorm \u7684 restful api \u7684\u5b8c\u6574\u793a\u4f8b\uff1a<br \/>\n<br \/>,\u73b0\u5728\uff0c\u60a8\u62e5\u6709\u4e86\u4e00\u4e2a\u5168\u9762\u7684<br \/>\n<strong>golang restful api<\/strong>\uff0c\u5b83\u6db5\u76d6\u4e86\u5404\u79cd postgresql \u529f\u80fd\uff0c\u4f7f\u5176\u6210\u4e3a\u5b66\u4e60\u6216\u9762\u8bd5\u7684\u5f3a\u5927\u793a\u4f8b\u3002,\u8ba9\u6211\u4eec\u901a\u8fc7\u5408\u5e76<br \/>\n<strong>\u89c6\u56fe<\/strong>\u3001<br \/>\n<strong>cte\uff08\u901a\u7528\u8868\u8868\u8fbe\u5f0f\uff09<\/strong>\u3001<br \/>\n<strong>\u5168\u6587\u7d22\u5f15\u6765\u589e\u5f3a <\/strong>golang restful api<br \/>\n<strong> \u793a\u4f8b\u4ee5\u53ca\u5176\u4ed6 postgresql \u529f\u80fd<\/strong> \u548c<br \/>\n<strong>json \u5904\u7406<\/strong>\u3002\u8fd9\u4e9b\u529f\u80fd\u4e2d\u7684\u6bcf\u4e00\u4e2a\u90fd\u5c06\u901a\u8fc7\u76f8\u5173\u7684 postgresql \u8868\u5b9a\u4e49\u548c\u4e0e\u5b83\u4eec\u4ea4\u4e92\u7684 golang \u4ee3\u7801\u8fdb\u884c\u6f14\u793a\u3002,\u8fd9\u90e8\u5206\u7684\u6570\u636e\u67b6\u6784\u5df2\u7ecf\u5728\u4e0a\u4e00\u8282\u4e2d\u51c6\u5907\u597d\u4e86\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u9700\u8981\u6dfb\u52a0\u66f4\u591a\u7684 golang \u4ee3\u7801\u5373\u53ef\u3002<br \/>\n<br \/>,\u5728\u8fd9\u4e2a\u6269\u5c55\u793a\u4f8b\u4e2d\uff0c\u6211\u5c06\u5c55\u793a\u5982\u4f55\u4f7f\u7528 golang \u548c postgresql \u96c6\u6210\u4ee5\u4e0b\u529f\u80fd\uff1a,vacuum \u901a\u5e38\u7528\u4f5c\u7ef4\u62a4\u4efb\u52a1\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u6765\u81ea\u5e94\u7528\u7a0b\u5e8f\u4ee3\u7801\u3002\u4f46\u662f\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 gorm \u7684 exec \u6765\u8fd0\u884c\u5b83\u4ee5\u8fdb\u884c\u5185\u52a1\u7ba1\u7406\uff1a<br \/>\n<br \/>,postgresql \u7684 mvcc \u901a\u8fc7\u4fdd\u7559\u4e0d\u540c\u7248\u672c\u7684\u884c\u6765\u5141\u8bb8<br \/>\n<strong>\u5e76\u53d1\u4e8b\u52a1<\/strong>\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u4e8b\u52a1\u5728 golang \u4e2d\u6f14\u793a mvcc \u884c\u4e3a\u7684\u793a\u4f8b\uff1a<br \/>\n<br \/>,\u7a97\u53e3\u51fd\u6570\u7528\u4e8e\u5bf9\u4e0e\u5f53\u524d\u884c\u76f8\u5173\u7684\u4e00\u7ec4\u8868\u884c\u6267\u884c\u8ba1\u7b97\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u7a97\u53e3\u51fd\u6570\u8ba1\u7b97\u6bcf\u4f4d\u4f5c\u8005<br \/>\n<strong>\u501f\u9605\u4e66\u7c4d<\/strong>\u7684\u8fd0\u884c\u603b\u6570\u7684\u793a\u4f8b\uff1a<br \/>\n<br \/>,\u4ee5\u4e0a\u5c31\u662f\u300aGolang RESTful API \u4e0e Gin\u3001Gorm\u3001PostgreSQL\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8golang\u5b66\u4e60\u7f51\u516c\u4f17\u53f7\uff01,<\/p>\n<p>\u4f7f\u7528<br \/>\u542f\u52a8golang\u670d\u52a1\u5668 <\/p>\n<pre> go run main.go\n<\/pre>\n<p>,\u8fd0\u884c postgresql sql \u811a\u672c\u6765\u521b\u5efa\u8868\u3001\u7d22\u5f15\u3001\u89c6\u56fe\u3001\u51fd\u6570\u548c\u89e6\u53d1\u5668\u3002,<br \/>\n<strong>\u8def\u7531<\/strong>\uff1a\u5b9a\u4e49\u521b\u5efa\u4f5c\u8005\u3001\u4e66\u7c4d\u3001\u7528\u6237\u3001\u501f\u9605\u4e66\u7c4d\u4ee5\u53ca\u83b7\u53d6\u501f\u9605\u8ba1\u6570\u7684\u8def\u7531\u3002,<br \/>\n<strong>\u6570\u636e\u5e93\u521d\u59cb\u5316<\/strong>\uff1a\u8fde\u63a5postgresql\u6570\u636e\u5e93\u5e76\u521d\u59cb\u5316gorm\u3002,<br \/>\n<strong>\u4e8b\u52a1\u5904\u7406<\/strong>\uff1a\u501f\u4e66\u65f6\u4f7f\u7528\u4e8b\u52a1\u6765\u786e\u4fdd\u4e00\u81f4\u6027\u3002,golang restful api,golang\u5b66\u4e60\u7f51\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGolang RESTful API \u4e0e Gin\u3001Gorm\u3001PostgreSQL\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>Golang RESTful API \u4e0e Gin\u3001Gorm\u3001PostgreSQL<\/span><\/p>\n<h1>Golang RESTful API \u4e0e Gin\u3001Gorm\u3001PostgreSQL<\/h1>\n<p><span>\u6765\u6e90\uff1adev.to<\/span><br \/>\n<span>2024-10-27 19:30:56<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>golang\u5b66\u4e60\u7f51\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGolang RESTful API \u4e0e Gin\u3001Gorm\u3001PostgreSQL\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730028641671e246137122.jpg\" class=\"aligncenter\" title=\"Golang RESTful API \u4e0e Gin\u3001Gorm\u3001PostgreSQL\u63d2\u56fe\" alt=\"Golang RESTful API \u4e0e Gin\u3001Gorm\u3001PostgreSQL\u63d2\u56fe\" \/><\/p>\n<p><strong>golang restful api<\/strong> \u670d\u52a1\u7684\u7efc\u5408\u793a\u4f8b\uff0c\u8be5\u670d\u52a1\u4f7f\u7528 gin \u8fdb\u884c\u8def\u7531\u3001gorm \u8fdb\u884c orm \u4ee5\u53ca postgresql \u4f5c\u4e3a\u6570\u636e\u5e93\u3002\u6b64\u793a\u4f8b\u5305\u62ec\u4ee5\u4e0b postgresql \u529f\u80fd\uff1a\u6570\u636e\u5e93\u548c\u8868\u521b\u5efa\u3001\u6570\u636e\u63d2\u5165\u548c\u67e5\u8be2\u3001\u7d22\u5f15\u3001\u51fd\u6570\u548c\u5b58\u50a8\u8fc7\u7a0b\u3001\u89e6\u53d1\u5668\u3001\u89c6\u56fe\u3001cte\u3001\u4e8b\u52a1\u3001\u7ea6\u675f\u548c json \u5904\u7406\u3002<\/p>\n<h3> 1. \u9879\u76ee\u8bbe\u7f6e <\/h3>\n<p>\u5047\u8bbe\u60a8\u5df2\u8bbe\u7f6e postgresql\u3001golang \u548c go mod\uff0c\u8bf7\u521d\u59cb\u5316\u9879\u76ee\uff1a<\/p>\n<pre>mkdir library-api\ncd library-api\ngo mod init library-api\n<\/pre>\n<p>\u9879\u76ee\u7ed3\u6784<\/p>\n<pre>\/library-api\n|-- db.sql\n|-- main.go\n|-- go.mod\n<\/pre>\n<h3> 2.\u5b89\u88c5\u4f9d\u8d56\u9879 <\/h3>\n<p>\u5b89\u88c5\u5fc5\u8981\u7684\u8f6f\u4ef6\u5305\uff1a<\/p>\n<pre>go get github.com\/gin-gonic\/gin\ngo get gorm.io\/gorm\ngo get gorm.io\/driver\/postgres\n<\/pre>\n<h3> 3.postgresql \u67b6\u6784 <\/h3>\n<p>\u8fd9\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u6570\u636e\u5e93\u6a21\u5f0f\u7684 sql \u811a\u672c\uff1a<\/p>\n<pre>-- create the library database.\ncreate database library;\n\n-- connect to the library database.\n\\c library;\n\n-- create tables.\ncreate table authors (\n    id serial primary key,\n    name varchar(100) not null unique,\n    bio text\n);\n\ncreate table books (\n    id serial primary key,\n    title varchar(200) not null,\n    -- this creates a foreign key constraint:\n    -- 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.\n    -- 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.\n    author_id integer references authors(id) on delete cascade,\n    published_date date not null,\n    description text,\n    details jsonb\n);\n\ncreate table users (\n    id serial primary key,\n    name varchar(100) not null,\n    email varchar(100) unique not null,\n    created_at timestamp default current_timestamp\n);\n\n-- create table borrow_logs (\n--     id serial primary key,\n--     user_id integer references users(id),\n--     book_id integer references books(id),\n--     borrowed_at timestamp default current_timestamp,\n--     returned_at timestamp\n-- );\n\n-- create a partitioned table for borrow logs based on year.\n-- the borrow_logs table is partitioned by year using partition by range (borrowed_at).\ncreate table borrow_logs (\n    id serial primary key,\n    user_id integer references users(id),\n    book_id integer references books(id),\n    borrowed_at timestamp default current_timestamp,\n    returned_at timestamp\n) partition by range (borrowed_at);\n\n-- create partitions for each year.\n-- automatic routing: postgresql automatically directs insert operations to the appropriate partition (borrow_logs_2023 or borrow_logs_2024) based on the borrowed_at date.\ncreate table borrow_logs_2023 partition of borrow_logs\n    for values from ('2023-01-01') to ('2024-01-01');\n\ncreate table borrow_logs_2024 partition of borrow_logs\n    for values from ('2024-01-01') to ('2025-01-01');\n-- benefit: this helps in improving query performance and managing large datasets by ensuring that data for each year is stored separately.\n\n\n\n-- indexes for faster searching.\ncreate index idx_books_published_date on books (published_date);\ncreate index idx_books_details on books using gin (details);\n-- gin index (generalized inverted index).  it is particularly useful for indexing columns with complex data types like arrays, jsonb, or text search fields\n\n\n-- add a full-text index to the title and description of books\ncreate index book_text_idx on books using gin (to_tsvector('english', title || ' ' || description));\n-- to_tsvector('english', ...) converts the concatenated title and description fields into a text search vector (tsv) suitable for full-text searching.\n-- the || operator concatenates the title and description fields, so both fields are indexed together for searching.\n-- 'english' specifies the language dictionary, which helps with stemming and stop-word filtering.\n\n\n-- create a simple view for books with author information.\ncreate view book_author_view as\nselect books.id as book_id, books.title, authors.name as author_name\nfrom books\njoin authors on books.author_id = authors.id;\n\n-- create a view to get user borrow history\ncreate view user_borrow_history as\nselect\n    u.id as user_id,\n    u.name as user_name,\n    b.title as book_title,\n    bl.borrowed_at,\n    bl.returned_at\nfrom\n    users u\n    join borrow_logs bl on u.id = bl.user_id\n    join books b on bl.book_id = b.id;\n\n-- use a cte to get all active borrow logs (not yet returned)\nwith active_borrows as (\n    select * from borrow_logs where returned_at is null\n)\nselect * from active_borrows;\n\n-- function to calculate the number of books borrowed by a user.\n-- creates a function that takes an int parameter user_id and returns an int value. if the function already exists, it will replace it.\ncreate or replace function get_borrow_count(user_id int) returns int as $$\n    -- $1 is a placeholder for the first input. when the function is executed, postgresql replaces $1 with the actual user_id value that is passed in by the caller.\n    select count(*) from borrow_logs where user_id = $1;\n$$ language sql;\n-- as $$ ... $$: this defines the body of the function between the dollar signs ($$).\n-- language sql: specifies that the function is written in sql.\n\n\n-- trigger to log activities.\ncreate table activity_logs (\n    id serial primary key,\n    description text,\n    created_at timestamp default current_timestamp\n);\n\ncreate or replace function log_activity() returns trigger as $$\nbegin\n    insert into activity_logs (description)\n    -- new refers to the new row being inserted or modified by the triggering event.\n    values ('a borrow_log entry has been added with id ' || new.id);\n    -- the function returns new, which means that the new data will be used as it is after the trigger action.\n    return new;\nend;\n$$ language plpgsql;\n-- it uses plpgsql, which is a procedural language in postgresql\n\ncreate trigger log_borrow_activity\nafter insert on borrow_logs\nfor each row execute function log_activity();\n\n-- add a jsonb column to store metadata\nalter table books add column metadata jsonb;\n-- example metadata: {\"tags\": [\"fiction\", \"bestseller\"], \"page_count\": 320}\n<\/pre>\n<h3> 4.go\u8bed\u8a00\u4ee3\u7801 <\/h3>\n<p>\u8fd9\u662f\u4f7f\u7528 gin \u548c gorm \u7684 restful api \u7684\u5b8c\u6574\u793a\u4f8b\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"net\/http\"\n    \"time\"\n\n    \"github.com\/gin-gonic\/gin\"\n    \"gorm.io\/driver\/postgres\"\n    \"gorm.io\/gorm\"\n)\n\ntype author struct {\n    id   uint   `gorm:\"primarykey\"`\n    name string `gorm:\"not null;unique\"`\n    bio  string\n}\n\ntype book struct {\n    id            uint                   `gorm:\"primarykey\"`\n    title         string                 `gorm:\"not null\"`\n    authorid      uint                   `gorm:\"not null\"`\n    publisheddate time.time              `gorm:\"not null\"`\n    details       map[string]interface{} `gorm:\"type:jsonb\"`\n}\n\ntype user struct {\n    id        uint   `gorm:\"primarykey\"`\n    name      string `gorm:\"not null\"`\n    email     string `gorm:\"not null;unique\"`\n    createdat time.time\n}\n\ntype borrowlog struct {\n    id         uint      `gorm:\"primarykey\"`\n    userid     uint      `gorm:\"not null\"`\n    bookid     uint      `gorm:\"not null\"`\n    borrowedat time.time `gorm:\"default:current_timestamp\"`\n    returnedat *time.time\n}\n\nvar db *gorm.db\n\nfunc initdb() {\n    dsn := \"host=localhost user=postgres password=yourpassword dbname=library port=5432 sslmode=disable\"\n    var err error\n    db, err = gorm.open(postgres.open(dsn), &amp;gorm.config{})\n    if err != nil {\n        panic(\"failed to connect to database\")\n    }\n\n    \/\/ auto-migrate models.\n    db.automigrate(&amp;author{}, &amp;book{}, &amp;user{}, &amp;borrowlog{})\n}\n\nfunc main() {\n    initdb()\n    r := gin.default()\n\n    r.post(\"\/authors\", createauthor)\n    r.post(\"\/books\", createbook)\n    r.post(\"\/users\", createuser)\n    r.post(\"\/borrow\", borrowbook)\n    r.get(\"\/borrow\/:id\", getborrowcount)\n    r.get(\"\/books\", listbooks)\n\n    r.run(\":8080\")\n}\n\nfunc createauthor(c *gin.context) {\n    var author author\n    if err := c.shouldbindjson(&amp;author); err != nil {\n        c.json(http.statusbadrequest, gin.h{\"error\": err.error()})\n        return\n    }\n    if err := db.create(&amp;author).error; err != nil {\n        c.json(http.statusinternalservererror, gin.h{\"error\": err.error()})\n        return\n    }\n    c.json(http.statusok, author)\n}\n\nfunc createbook(c *gin.context) {\n    var book book\n    if err := c.shouldbindjson(&amp;book); err != nil {\n        c.json(http.statusbadrequest, gin.h{\"error\": err.error()})\n        return\n    }\n    if err := db.create(&amp;book).error; err != nil {\n        c.json(http.statusinternalservererror, gin.h{\"error\": err.error()})\n        return\n    }\n    c.json(http.statusok, book)\n}\n\nfunc createuser(c *gin.context) {\n    var user user\n    if err := c.shouldbindjson(&amp;user); err != nil {\n        c.json(http.statusbadrequest, gin.h{\"error\": err.error()})\n        return\n    }\n    if err := db.create(&amp;user).error; err != nil {\n        c.json(http.statusinternalservererror, gin.h{\"error\": err.error()})\n        return\n    }\n    c.json(http.statusok, user)\n}\n\n\/\/ the golang code does not need changes specifically to use the partitioned tables; the partitioning is handled by postgresql\n\/\/ you simply insert into the borrow_logs table, and postgresql will automatically route the data to the correct partition.\nfunc borrowbook(c *gin.context) {\n    var log borrowlog\n    if err := c.shouldbindjson(&amp;log); err != nil {\n        c.json(http.statusbadrequest, gin.h{\"error\": err.error()})\n        return\n    }\n\n    tx := db.begin()\n    if err := tx.create(&amp;log).error; err != nil {\n        tx.rollback()\n        c.json(http.statusinternalservererror, gin.h{\"error\": err.error()})\n        return\n    }\n    tx.commit()\n    c.json(http.statusok, log)\n}\n\nfunc getborrowcount(c *gin.context) {\n    userid := c.param(\"id\")\n    var count int\n    if err := db.raw(\"select get_borrow_count(?)\", userid).scan(&amp;count).error; err != nil {\n        c.json(http.statusinternalservererror, gin.h{\"error\": err.error()})\n        return\n    }\n    c.json(http.statusok, gin.h{\"borrow_count\": count})\n}\n\n\/\/ when querying a partitioned table in postgresql using golang, no changes are needed in the query logic or code.\n\/\/ 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.\n\/\/ performance: postgresql optimizes the query by scanning only the relevant partitions, which can significantly speed up queries when dealing with large datasets.\n\/\/ here\u2019s how you might query the borrow_logs table using gorm, even though it\u2019s partitioned:\nfunc getborrowlogs(c *gin.context) {\n    var logs []borrowlog\n    if err := db.where(\"user_id = ?\", c.param(\"user_id\")).find(&amp;logs).error; err != nil {\n        c.json(http.statusinternalservererror, gin.h{\"error\": err.error()})\n        return\n    }\n    c.json(http.statusok, logs)\n}\n\nfunc listbooks(c *gin.context) {\n    var books []book\n    db.preload(\"author\").find(&amp;books)\n    c.json(http.statusok, books)\n}\n<\/pre>\n<h3> golang\u4ee3\u7801\u8bf4\u660e\uff1a <\/h3>\n<ul>\n<li> <strong>\u6570\u636e\u5e93\u521d\u59cb\u5316<\/strong>\uff1a\u8fde\u63a5postgresql\u6570\u636e\u5e93\u5e76\u521d\u59cb\u5316gorm\u3002<\/li>\n<li> <strong>\u8def\u7531<\/strong>\uff1a\u5b9a\u4e49\u521b\u5efa\u4f5c\u8005\u3001\u4e66\u7c4d\u3001\u7528\u6237\u3001\u501f\u9605\u4e66\u7c4d\u4ee5\u53ca\u83b7\u53d6\u501f\u9605\u8ba1\u6570\u7684\u8def\u7531\u3002<\/li>\n<li> <strong>\u4e8b\u52a1\u5904\u7406<\/strong>\uff1a\u501f\u4e66\u65f6\u4f7f\u7528\u4e8b\u52a1\u6765\u786e\u4fdd\u4e00\u81f4\u6027\u3002<\/li>\n<li> <strong>preload<\/strong>\uff1a\u4f7f\u7528 gorm \u7684 preload \u6765\u8fde\u63a5\u76f8\u5173\u8868\uff08\u4f5c\u8005\u4e0e\u4e66\u7c4d\uff09\u3002<\/li>\n<li> <strong>\u5b58\u50a8\u8fc7\u7a0b\u8c03\u7528<\/strong>\uff1a\u4f7f\u7528 db.raw \u8c03\u7528\u81ea\u5b9a\u4e49 postgresql \u51fd\u6570\u6765\u8ba1\u7b97\u501f\u7528\u8ba1\u6570\u3002<\/li>\n<\/ul>\n<h3> 5. \u8fd0\u884capi <\/h3>\n<ul>\n<li>\u8fd0\u884c postgresql sql \u811a\u672c\u6765\u521b\u5efa\u8868\u3001\u7d22\u5f15\u3001\u89c6\u56fe\u3001\u51fd\u6570\u548c\u89e6\u53d1\u5668\u3002<\/li>\n<li>\n<p>\u4f7f\u7528<br \/>\u542f\u52a8golang\u670d\u52a1\u5668 <\/p>\n<pre> go run main.go\n<\/pre>\n<\/li>\n<\/ul>\n<p>\u73b0\u5728\uff0c\u60a8\u62e5\u6709\u4e86\u4e00\u4e2a\u5168\u9762\u7684<strong>golang restful api<\/strong>\uff0c\u5b83\u6db5\u76d6\u4e86\u5404\u79cd postgresql \u529f\u80fd\uff0c\u4f7f\u5176\u6210\u4e3a\u5b66\u4e60\u6216\u9762\u8bd5\u7684\u5f3a\u5927\u793a\u4f8b\u3002<\/p>\n<h3> 6.\u6dfb\u52a0\u66f4\u591a\u529f\u80fd\u3002 <\/h3>\n<p>\u8ba9\u6211\u4eec\u901a\u8fc7\u5408\u5e76 <strong>\u89c6\u56fe<\/strong>\u3001<strong>cte\uff08\u901a\u7528\u8868\u8868\u8fbe\u5f0f\uff09<\/strong>\u3001<strong>\u5168\u6587\u7d22\u5f15\u6765\u589e\u5f3a <\/strong>golang restful api<strong> \u793a\u4f8b\u4ee5\u53ca\u5176\u4ed6 postgresql \u529f\u80fd<\/strong> \u548c <strong>json \u5904\u7406<\/strong>\u3002\u8fd9\u4e9b\u529f\u80fd\u4e2d\u7684\u6bcf\u4e00\u4e2a\u90fd\u5c06\u901a\u8fc7\u76f8\u5173\u7684 postgresql \u8868\u5b9a\u4e49\u548c\u4e0e\u5b83\u4eec\u4ea4\u4e92\u7684 golang \u4ee3\u7801\u8fdb\u884c\u6f14\u793a\u3002<\/p>\n<p>\u8fd9\u90e8\u5206\u7684\u6570\u636e\u67b6\u6784\u5df2\u7ecf\u5728\u4e0a\u4e00\u8282\u4e2d\u51c6\u5907\u597d\u4e86\uff0c\u6240\u4ee5\u6211\u4eec\u53ea\u9700\u8981\u6dfb\u52a0\u66f4\u591a\u7684 golang \u4ee3\u7801\u5373\u53ef\u3002<\/p>\n<pre>\/\/ querying the user_borrow_history view:\nfunc getuserborrowhistory(c *gin.context) {\n    var history []struct {\n        userid     uint       `json:\"user_id\"`\n        username   string     `json:\"user_name\"`\n        booktitle  string     `json:\"book_title\"`\n        borrowedat time.time  `json:\"borrowed_at\"`\n        returnedat *time.time `json:\"returned_at,omitempty\"`\n    }\n\n    if err := db.raw(\"select * from user_borrow_history where user_id = ?\", c.param(\"user_id\")).scan(&amp;history).error; err != nil {\n        c.json(http.statusinternalservererror, gin.h{\"error\": err.error()})\n        return\n    }\n\n    c.json(http.statusok, history)\n}\n\n\/\/ using a cte in a query:\nfunc getactiveborrows(c *gin.context) {\n    var logs []borrowlog\n    query := `\n    with active_borrows as (\n        select * from borrow_logs where returned_at is null\n    )\n    select * from active_borrows where user_id = ?`\n\n    if err := db.raw(query, c.param(\"user_id\")).scan(&amp;logs).error; err != nil {\n        c.json(http.statusinternalservererror, gin.h{\"error\": err.error()})\n        return\n    }\n\n    c.json(http.statusok, logs)\n}\n\n\/\/ full-text search in books:\nfunc searchbooks(c *gin.context) {\n    var books []book\n    searchquery := c.query(\"q\")\n    query := `\n    select * from books\n    where to_tsvector('english', title || ' ' || description) @@ plainto_tsquery('english', ?)\n    `\n\n    if err := db.raw(query, searchquery).scan(&amp;books).error; err != nil {\n        c.json(http.statusinternalservererror, gin.h{\"error\": err.error()})\n        return\n    }\n\n    c.json(http.statusok, books)\n}\n\n\/\/ handling jsonb data:\nfunc updatebookmetadata(c *gin.context) {\n    var metadata map[string]interface{}\n    if err := c.shouldbindjson(&amp;metadata); err != nil {\n        c.json(http.statusbadrequest, gin.h{\"error\": err.error()})\n        return\n    }\n\n    bookid := c.param(\"book_id\")\n    if err := db.model(&amp;book{}).where(\"id = ?\", bookid).update(\"metadata\", metadata).error; err != nil {\n        c.json(http.statusinternalservererror, gin.h{\"error\": err.error()})\n        return\n    }\n\n    c.json(http.statusok, gin.h{\"status\": \"metadata updated\"})\n}\n\n\/\/ can query a specific field from a jsonb column using the -&gt;&gt; operator to extract a value as text:\nfunc getbooktags(c *gin.context) {\n    var tags []string\n    bookid := c.param(\"book_id\")\n    query := `select metadata-&gt;&gt;'tags' from books where id = ?`\n\n    if err := db.raw(query, bookid).scan(&amp;tags).error; err != nil {\n        c.json(http.statusinternalservererror, gin.h{\"error\": err.error()})\n        return\n    }\n\n    c.json(http.statusok, gin.h{\"tags\": tags})\n}\n\n\/\/ to add or update fields in a jsonb column, use the jsonb_set function:\nfunc updatebookpagecount(c *gin.context) {\n    bookid := c.param(\"book_id\")\n    var input struct {\n        pagecount int `json:\"page_count\"`\n    }\n\n    if err := c.shouldbindjson(&amp;input); err != nil {\n        c.json(http.statusbadrequest, gin.h{\"error\": err.error()})\n        return\n    }\n\n    query := `\n    update books\n    set metadata = jsonb_set(metadata, '{page_count}', to_jsonb(?::int), true)\n    where id = ?`\n\n    if err := db.exec(query, input.pagecount, bookid).error; err != nil {\n        c.json(http.statusinternalservererror, gin.h{\"error\": err.error()})\n        return\n    }\n\n    c.json(http.statusok, gin.h{\"status\": \"page count updated\"})\n}\n<\/pre>\n<h4> \u7279\u70b9\u603b\u7ed3\uff1a <\/h4>\n<ul>\n<li> <strong>\u89c6\u56fe<\/strong>\uff1a\u4f7f\u7528 user_borrow_history \u89c6\u56fe\u7b80\u5316\u5bf9\u6570\u636e\u7684\u8bbf\u95ee\uff0c\u4f7f\u590d\u6742\u7684\u8054\u63a5\u66f4\u6613\u4e8e\u67e5\u8be2\u3002<\/li>\n<li> <strong>cte<\/strong>\uff1a\u4f7f\u7528with\u5b50\u53e5\u8fdb\u884c\u6709\u7ec4\u7ec7\u7684\u67e5\u8be2\uff0c\u4f8b\u5982\u83b7\u53d6\u6d3b\u52a8\u501f\u7528\u65e5\u5fd7\u3002<\/li>\n<li> <strong>\u5168\u6587\u7d22\u5f15<\/strong>\uff1a\u901a\u8fc7 to_tsvector \u4e0a\u7684 gin \u7d22\u5f15\u589e\u5f3a\u4e66\u7c4d\u7684\u641c\u7d22\u80fd\u529b\u3002<\/li>\n<li>\n<p><strong>json \u5904\u7406<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 jsonb \u7c7b\u578b\u5b58\u50a8\u548c\u66f4\u65b0\u4e30\u5bcc\u7684\u5143\u6570\u636e\u3002<\/li>\n<li> getbooktags \u4ece\u5143\u6570\u636e jsonb \u5217\u4e2d\u68c0\u7d22\u7279\u5b9a\u7684 json \u5b57\u6bb5\uff08\u6807\u7b7e\uff09\u3002<\/li>\n<li> updatebookpagecount \u66f4\u65b0\u6216\u6dfb\u52a0\u5143\u6570\u636e jsonb \u5217\u4e2d\u7684 page_count \u5b57\u6bb5\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u5c06 db.raw \u548c db.exec \u7528\u4e8e gorm \u7684\u539f\u59cb sql\uff0c\u60a8\u53ef\u4ee5\u5229\u7528 postgresql \u7684\u5f3a\u5927\u529f\u80fd\uff0c\u540c\u65f6\u4e3a\u5e94\u7528\u7a0b\u5e8f\u7684\u5176\u4ed6\u90e8\u5206\u4fdd\u7559 gorm \u7684 orm \u529f\u80fd\u3002\u8fd9\u4f7f\u5f97\u8be5\u89e3\u51b3\u65b9\u6848\u65e2\u7075\u6d3b\u53c8\u529f\u80fd\u4e30\u5bcc\u3002<\/p>\n<\/li>\n<\/ul>\n<h3> 7.\u5176\u4ed6\u9ad8\u7ea7\u529f\u80fd <\/h3>\n<p>\u5728\u8fd9\u4e2a\u6269\u5c55\u793a\u4f8b\u4e2d\uff0c\u6211\u5c06\u5c55\u793a\u5982\u4f55\u4f7f\u7528 golang \u548c postgresql \u96c6\u6210\u4ee5\u4e0b\u529f\u80fd\uff1a<\/p>\n<ol>\n<li> <strong>vacuum<\/strong>\uff1a\u7528\u4e8e\u56de\u6536\u6b7b\u5143\u7ec4\u5360\u7528\u7684\u5b58\u50a8\u5e76\u9632\u6b62\u8868\u81a8\u80c0\u3002<\/li>\n<li> <strong>mvcc<\/strong>\uff1a\u901a\u8fc7\u7ef4\u62a4\u4e0d\u540c\u7248\u672c\u7684\u884c\u6765\u5141\u8bb8\u5e76\u53d1\u4e8b\u52a1\u7684\u6982\u5ff5\u3002<\/li>\n<li> <strong>\u7a97\u53e3\u51fd\u6570<\/strong>\uff1a\u7528\u4e8e\u5728\u4e0e\u5f53\u524d\u884c\u76f8\u5173\u7684\u4e00\u7ec4\u8868\u884c\u4e0a\u6267\u884c\u8ba1\u7b97\u3002<\/li>\n<\/ol>\n<h4> 1. \u5728golang\u4e2d\u4f7f\u7528vacuum <\/h4>\n<p>vacuum \u901a\u5e38\u7528\u4f5c\u7ef4\u62a4\u4efb\u52a1\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u6765\u81ea\u5e94\u7528\u7a0b\u5e8f\u4ee3\u7801\u3002\u4f46\u662f\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 gorm \u7684 exec \u6765\u8fd0\u884c\u5b83\u4ee5\u8fdb\u884c\u5185\u52a1\u7ba1\u7406\uff1a<\/p>\n<pre>func vacuumbooks(c *gin.context) {\n    if err := db.exec(\"vacuum analyze books\").error; err != nil {\n        c.json(http.statusinternalservererror, gin.h{\"error\": err.error()})\n        return\n    }\n    c.json(http.statusok, gin.h{\"status\": \"vacuum performed successfully\"})\n}\n<\/pre>\n<ul>\n<li> vacuum analyze books\uff1a\u56de\u6536\u5b58\u50a8\u5e76\u66f4\u65b0\u67e5\u8be2\u89c4\u5212\u5668\u4e3a books \u8868\u4f7f\u7528\u7684\u7edf\u8ba1\u4fe1\u606f\u3002<\/li>\n<li>\u8fd0\u884c vacuum \u901a\u5e38\u662f\u5728\u975e\u9ad8\u5cf0\u65f6\u6bb5\u6216\u4f5c\u4e3a\u7ef4\u62a4\u811a\u672c\u7684\u4e00\u90e8\u5206\u800c\u4e0d\u662f\u9488\u5bf9\u6bcf\u4e2a\u8bf7\u6c42\u8fd0\u884c\u3002<\/li>\n<\/ul>\n<h4> 2.\u4e86\u89e3mvcc\uff08\u591a\u7248\u672c\u5e76\u53d1\u63a7\u5236\uff09 <\/h4>\n<p>postgresql \u7684 mvcc \u901a\u8fc7\u4fdd\u7559\u4e0d\u540c\u7248\u672c\u7684\u884c\u6765\u5141\u8bb8<strong>\u5e76\u53d1\u4e8b\u52a1<\/strong>\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u4e8b\u52a1\u5728 golang \u4e2d\u6f14\u793a mvcc \u884c\u4e3a\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>func updatebooktitle(c *gin.context) {\n    bookid := c.param(\"book_id\")\n    var input struct {\n        newtitle string `json:\"new_title\"`\n    }\n    if err := c.shouldbindjson(&amp;input); err != nil {\n        c.json(http.statusbadrequest, gin.h{\"error\": err.error()})\n        return\n    }\n\n    \/\/ start a transaction to demonstrate mvcc\n    tx := db.begin()\n\n    defer func() {\n        if r := recover(); r != nil {\n            tx.rollback()\n        }\n    }()\n\n    var book book\n    if err := tx.set(\"gorm:query_option\", \"for update\").first(&amp;book, bookid).error; err != nil {\n        tx.rollback()\n        c.json(http.statusnotfound, gin.h{\"error\": \"book not found\"})\n        return\n    }\n\n    \/\/ simulate an update to demonstrate mvcc handling\n    book.title = input.newtitle\n    if err := tx.save(&amp;book).error; err != nil {\n        tx.rollback()\n        c.json(http.statusinternalservererror, gin.h{\"error\": err.error()})\n        return\n    }\n\n    \/\/ commit the transaction\n    tx.commit()\n    c.json(http.statusok, gin.h{\"status\": \"book title updated\"})\n}\n<\/pre>\n<ul>\n<li> for update\uff1a\u5728\u4e8b\u52a1\u671f\u95f4\u9501\u5b9a\u8981\u66f4\u65b0\u7684\u9009\u5b9a\u884c\uff0c\u9632\u6b62\u5176\u4ed6\u4e8b\u52a1\u4fee\u6539\u5b83\uff0c\u76f4\u5230\u5f53\u524d\u4e8b\u52a1\u5b8c\u6210\u3002<\/li>\n<li>\u8fd9\u786e\u4fdd\u4e86\u5e76\u53d1\u8bbf\u95ee\u671f\u95f4\u7684\u4e00\u81f4\u6027\uff0c\u5c55\u793a\u4e86 mvcc \u5982\u4f55\u5141\u8bb8\u5e76\u53d1\u8bfb\u53d6\u4f46\u9501\u5b9a\u884c\u4ee5\u8fdb\u884c\u66f4\u65b0\u3002<\/li>\n<\/ul>\n<h4> 3. \u5c06\u7a97\u53e3\u51fd\u6570\u4e0e gorm \u7ed3\u5408\u4f7f\u7528 <\/h4>\n<p>\u7a97\u53e3\u51fd\u6570\u7528\u4e8e\u5bf9\u4e0e\u5f53\u524d\u884c\u76f8\u5173\u7684\u4e00\u7ec4\u8868\u884c\u6267\u884c\u8ba1\u7b97\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u7a97\u53e3\u51fd\u6570\u8ba1\u7b97\u6bcf\u4f4d\u4f5c\u8005<strong>\u501f\u9605\u4e66\u7c4d<\/strong>\u7684\u8fd0\u884c\u603b\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>func getAuthorBorrowStats(c *gin.Context) {\n    var stats []struct {\n        AuthorID       int    `json:\"author_id\"`\n        AuthorName     string `json:\"author_name\"`\n        TotalBorrows   int    `json:\"total_borrows\"`\n        RunningTotal   int    `json:\"running_total\"`\n    }\n\n    query := `\n    SELECT\n        a.id AS author_id,\n        a.name AS author_name,\n        COUNT(bl.id) AS total_borrows,\n        SUM(COUNT(bl.id)) OVER (PARTITION BY a.id ORDER BY bl.borrowed_at) AS running_total\n    FROM authors a\n    LEFT JOIN books b ON b.author_id = a.id\n    LEFT JOIN borrow_logs bl ON bl.book_id = b.id\n    GROUP BY a.id, a.name, bl.borrowed_at\n    ORDER BY a.id, bl.borrowed_at\n    `\n\n    if err := db.Raw(query).Scan(&amp;stats).Error; err != nil {\n        c.JSON(http.StatusInternalServerError, gin.H{\"error\": err.Error()})\n        return\n    }\n\n    c.JSON(http.StatusOK, stats)\n}\n<\/pre>\n<ul>\n<li> sum(count(bl.id)) over (partition by a.id order by bl.borrowed_at)\uff1a\u4e00\u4e2a\u7a97\u53e3\u51fd\u6570\uff0c\u8ba1\u7b97\u6bcf\u4e2a\u4f5c\u8005\u501f\u9605\u4e66\u7c4d\u7684\u8fd0\u884c\u603b\u6570\uff0c\u6309borrowed_at\u65e5\u671f\u6392\u5e8f\u3002<\/li>\n<li>\u8fd9\u53ef\u4ee5\u63d0\u4f9b\u89c1\u89e3\uff0c\u4f8b\u5982\u6bcf\u4e2a\u4f5c\u8005\u7684\u501f\u9605\u8d8b\u52bf\u5982\u4f55\u968f\u65f6\u95f4\u53d8\u5316\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aGolang RESTful API \u4e0e Gin\u3001Gorm\u3001PostgreSQL\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8golang\u5b66\u4e60\u7f51\u516c\u4f17\u53f7\uff01<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664  <\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u638c\u63e1 PHP \u51fd\u6570\u7684\u5185\u5b58\u7ba1\u7406\u6280\u5de7\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   \u5982\u4f55\u5728Java\u4e2d\u5b9a\u4e49\u516c\u6709\u51fd\u6570\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>golang\u5b66\u4e60\u7f51\u4eca\u5929\u5c06\u7ed9\u5927\u5bb6\u5e26\u6765 \u300aGolang RESTful API \u4e0e Gin\u3001Gorm\u3001PostgreSQL\u300b\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230 \u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u662f\u6b63\u5728\u5b66\u4e60 Golang\u6216\u8005\u5df2\u7ecf\u662f\u5927\u4f6c\u7ea7\u522b\u4e86\uff0c\u90fd\u975e\u5e38\u6b22\u8fce\u4e5f\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u7ed9\u6211\u5efa\u8bae\u8bc4\u8bba\u54c8~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff01, , golang restful api \u670d\u52a1\u7684\u7efc\u5408\u793a\u4f8b\uff0c\u8be5\u670d\u52a1\u4f7f\u7528 gin \u8fdb\u884c\u8def\u7531\u3001gorm \u8fdb\u884c orm \u4ee5\u53ca postgresql \u4f5c\u4e3a\u6570\u636e\u5e93\u3002\u6b64\u793a\u4f8b\u5305\u62ec\u4ee5\u4e0b postgresql \u529f\u80fd\uff1a\u6570\u636e\u5e93\u548c\u8868\u521b\u5efa\u3001\u6570\u636e\u63d2\u5165\u548c\u67e5\u8be2\u3001\u7d22\u5f15\u3001\u51fd\u6570\u548c\u5b58\u50a8\u8fc7\u7a0b\u3001\u89e6\u53d1\u5668\u3001\u89c6\u56fe\u3001cte\u3001\u4e8b\u52a1\u3001\u7ea6\u675f\u548c json \u5904\u7406\u3002,\u5047\u8bbe\u60a8\u5df2\u8bbe\u7f6e postgresql\u3001golang \u548c go mod\uff0c\u8bf7\u521d\u59cb\u5316\u9879\u76ee\uff1a ,\u9879\u76ee\u7ed3\u6784 ,\u5b89\u88c5\u5fc5\u8981\u7684\u8f6f\u4ef6\u5305\uff1a ,\u8fd9\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u6570\u636e\u5e93\u6a21\u5f0f\u7684 sql \u811a\u672c\uff1a ,\u8fd9\u662f\u4f7f\u7528 gin \u548c gorm \u7684 restful api \u7684\u5b8c\u6574\u793a\u4f8b\uff1a ,\u73b0\u5728\uff0c\u60a8\u62e5\u6709\u4e86\u4e00\u4e2a\u5168\u9762\u7684 golang restful api\uff0c\u5b83\u6db5\u76d6\u4e86\u5404\u79cd postgresql \u529f\u80fd\uff0c\u4f7f\u5176\u6210\u4e3a\u5b66\u4e60\u6216\u9762\u8bd5\u7684\u5f3a\u5927\u793a\u4f8b\u3002,\u8ba9\u6211\u4eec\u901a\u8fc7\u5408\u5e76 \u89c6\u56fe\u3001 cte\uff08\u901a\u7528\u8868\u8868\u8fbe\u5f0f\uff09\u3001 \u5168\u6587\u7d22\u5f15\u6765\u589e\u5f3a golang restful api \u793a\u4f8b\u4ee5\u53ca\u5176\u4ed6 postgresql [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-40762","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40762","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=40762"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/40762\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=40762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=40762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=40762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}