目錄
1. Define the Core Goals
2. Use Struct Tags for Mapping
3. Build a Simple Insert Function
4. Implement a Generic Select Function
5. Handle Primary Keys and Updates (Optional)
6. Use Interfaces for Extensibility
7. Limitations and Trade-offs
Final Thoughts
首頁 後端開發 Golang 在Go中建立自定義ORM

在Go中建立自定義ORM

Aug 02, 2025 pm 03:52 PM

定義核心目標:實現結構體到數據庫表的映射、自動生成SQL語句(INSERT、SELECT)、使用reflect進行結構體反射操作;2. 使用struct標籤如db:"column_name"將結構體字段映射到數據庫列;3. 構建Insert函數:通過反射遍歷結構體字段,提取標籤和值,動態生成INSERT語句並執行;4. 實現Select函數:通過反射創建結果切片元素,使用rows.Scan將查詢結果填充到結構體字段中;5. 可選支持主鍵和更新:通過擴展標籤如pk:"true"識別主鍵字段以生成WHERE條件;6. 使用接口提升擴展性:定義Model接口並通過TableName方法自動獲取表名;7. 認識局限性:缺乏JOIN、關係處理、遷移等功能,性能受反射影響,但適合學習或小型項目。構建自定義ORM的核心在於理解數據映射與SQL抽像機制,其目的不是替代成熟框架,而是掌握底層原理並滿足特定場景需求。

Building a Custom ORM in Go

Building a custom ORM (Object-Relational Mapper) in Go might sound like a daunting task, but it's a great way to understand how data layers work and how Go's powerful reflection and interface system can be leveraged to abstract SQL operations. While mature ORMs like GORM or SQLx exist, rolling your own—even as a learning exercise—gives you insight into what happens under the hood.

Building a Custom ORM in Go

Here's a practical breakdown of how to build a minimal but functional custom ORM in Go.


1. Define the Core Goals

Before writing any code, decide what your ORM should do. A basic ORM typically handles:

Building a Custom ORM in Go
  • Mapping structs to database tables
  • Automatically generating SQL queries (INSERT, SELECT, UPDATE, DELETE)
  • Scanning query results into structs
  • Handling basic relationships (optional, for advanced versions)

For this example, we'll focus on:

  • Struct-to-table mapping via tags
  • Insert and Select operations
  • Using database/sql with reflect for introspection

2. Use Struct Tags for Mapping

Go's reflect package allows you to inspect structs at runtime. Use struct tags to map fields to column names.

Building a Custom ORM in Go
 type User struct {
    ID int `db:"id"`
    Name string `db:"name"`
    Email string `db:"email"`
}

We'll use the db tag to indicate the corresponding database column.


3. Build a Simple Insert Function

Here's how to dynamically generate an INSERT query:

 import (
    "database/sql"
    "fmt"
    "reflect"
)

func Insert(db *sql.DB, tableName string, obj interface{}) error {
    v := reflect.ValueOf(obj).Elem()
    t := reflect.TypeOf(obj).Elem()

    var columns []string
    var placeholders []string
    var args []interface{}

    for i := 0; i < v.NumField(); i {
        field := t.Field(i)
        column := field.Tag.Get("db")
        if column == "" {
            continue
        }

        value := v.Field(i).Interface()
        columns = append(columns, column)
        placeholders = append(placeholders, "?")
        args = append(args, value)
    }

    query := fmt.Sprintf(
        "INSERT INTO %s (%s) VALUES (%s)",
        tableName,
        join(columns, ", "),
        join(placeholders, ", "),
    )

    _, err := db.Exec(query, args...)
    return err
}

Note: Use ? for MySQL/SQLite, $1 for PostgreSQL. Adjust placeholder style accordingly.


4. Implement a Generic Select Function

Now, let's scan results back into a struct:

 func Select(db *sql.DB, query string, dest interface{}) error {
    rows, err := db.Query(query)
    if err != nil {
        return err
    }
    defer rows.Close()

    destValue := reflect.ValueOf(dest).Elem()
    sliceType := destValue.Type().Elem() // element type of the slice

    var results []reflect.Value

    for rows.Next() {
        elem := reflect.New(sliceType).Elem()
        values := make([]interface{}, elem.NumField())

        for i := 0; i < elem.NumField(); i {
            field := elem.Type().Field(i)
            column := field.Tag.Get("db")
            if column == "" {
                continue
            }
            values[i] = elem.Field(i).Addr().Interface()
        }

        err := rows.Scan(values...)
        if err != nil {
            return err
        }

        results = append(results, elem)
    }

    for _, r := range results {
        destValue.Set(reflect.Append(destValue, r))
    }

    return rows.Err()
}

Usage:

 var users []User
err := Select(db, "SELECT id, name, email FROM users", &users)

5. Handle Primary Keys and Updates (Optional)

To support UPDATE , you need to know the primary key. You can extend the tag:

 ID int `db:"id" pk:"true"`

Then, during update, find the PK field and generate a WHERE clause.


6. Use Interfaces for Extensibility

Define a simple interface to make your ORM feel more cohesive:

 type Model interface {
    TableName() string
}

func (u User) TableName() string {
    return "users"
}

Now your Insert function can call obj.TableName() instead of requiring the table name as a parameter.


7. Limitations and Trade-offs

Building your own ORM means you'll face challenges:

  • No support for JOINs or relations (without extra work)
  • SQL injection risk if you don't use parameterized queries
  • Poor performance if reflection is overused
  • No migrations, hooks, or advanced querying

But for small projects or embedded tools, a lightweight ORM can be sufficient.


Final Thoughts

A custom ORM in Go is totally feasible thanks to reflect and strong typing. You don't need to replace GORM—this is more about learning how ORMs work, or creating a minimal data layer tailored to your app's needs.

Start small: support INSERT and SELECT on basic structs. Then incrementally add features like filters, updates, or transactions.

It's not about reinventing the wheel—it's about knowing how the wheel works.

Basically, that's how you start building your own ORM in Go.

以上是在Go中建立自定義ORM的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

Rimworld Odyssey溫度指南和Gravtech
1 個月前 By Jack chen
初學者的Rimworld指南:奧德賽
1 個月前 By Jack chen
PHP變量範圍解釋了
4 週前 By 百草
撰寫PHP評論的提示
3 週前 By 百草
在PHP中評論代碼
3 週前 By 百草

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Laravel 教程
1604
29
PHP教程
1509
276
如何在GO中構建Web服務器 如何在GO中構建Web服務器 Jul 15, 2025 am 03:05 AM

搭建一個用Go編寫的Web服務器並不難,核心在於利用net/http包實現基礎服務。 1.使用net/http啟動最簡服務器:通過幾行代碼註冊處理函數並監聽端口;2.路由管理:使用ServeMux組織多個接口路徑,便於結構化管理;3.常見做法:按功能模塊分組路由,並可用第三方庫支持複雜匹配;4.靜態文件服務:通過http.FileServer提供HTML、CSS和JS文件;5.性能與安全:啟用HTTPS、限制請求體大小、設置超時時間以提升安全性與性能。掌握這些要點後,擴展功能將更加容易。

進行音頻/視頻處理 進行音頻/視頻處理 Jul 20, 2025 am 04:14 AM

音視頻處理的核心在於理解基本流程與優化方法。 1.其基本流程包括採集、編碼、傳輸、解碼和播放,每個環節均有技術難點;2.常見問題如音畫不同步、卡頓延遲、聲音噪音、畫面模糊等,可通過同步調整、編碼優化、降噪模塊、參數調節等方式解決;3.推薦使用FFmpeg、OpenCV、WebRTC、GStreamer等工具實現功能;4.性能管理方面應注重硬件加速、合理設置分辨率幀率、控制並發及內存洩漏問題。掌握這些關鍵點有助於提升開發效率和用戶體驗。

使用默認情況選擇 使用默認情況選擇 Jul 14, 2025 am 02:54 AM

select加default的作用是讓select在沒有其他分支就緒時執行默認行為,避免程序阻塞。 1.非阻塞地從channel接收數據時,若channel為空,會直接進入default分支;2.結合time.After或ticker定時嘗試發送數據,若channel滿則不阻塞而跳過;3.防止死鎖,在不確定channel是否被關閉時避免程序卡住;使用時需注意default分支會立即執行,不能濫用,且default與case互斥,不會同時執行。

在GO中開發Kubernetes運營商 在GO中開發Kubernetes運營商 Jul 25, 2025 am 02:38 AM

編寫KubernetesOperator的最有效方式是使用Go語言結合Kubebuilder和controller-runtime。 1.理解Operator模式:通過CRD定義自定義資源,編寫控制器監聽資源變化並執行調和循環以維護期望狀態。 2.使用Kubebuilder初始化項目並創建API,自動生成CRD、控制器和配置文件。 3.在api/v1/myapp_types.go中定義CRD的Spec和Status結構體,運行makemanifests生成CRDYAML。 4.在控制器的Reconcil

去休息API示例 去休息API示例 Jul 14, 2025 am 03:01 AM

如何快速實現一個Go編寫的RESTAPI示例?答案是使用net/http標準庫,按照以下三個步驟即可完成:1.設置項目結構並初始化模塊;2.定義數據結構和處理函數,包括獲取所有數據、根據ID獲取單個數據、創建新數據;3.在main函數中註冊路由並啟動服務器。整個過程無需第三方庫,通過標準庫即可實現基本的RESTAPI功能,並可通過瀏覽器或Postman進行測試。

如何在Go中提出HTTP請求 如何在Go中提出HTTP請求 Jul 14, 2025 am 02:48 AM

在Go語言中發起HTTP請求的方法如下:1.使用http.Get()發起最簡單的GET請求,記得處理錯誤並關閉Body;2.使用http.Post()或http.NewRequest() http.Client.Do()發送POST請求,可設置JSON數據或表單數據;3.設置超時、Header和Cookie,通過Client控制Timeout、Header.Set添加自定義頭,以及使用CookieJar自動管理Cookie;4.注意事項包括必須關閉Body、不可複用req對象、設置User-Ag

Go PostgreSQL/MySQL的查詢優化技術 Go PostgreSQL/MySQL的查詢優化技術 Jul 19, 2025 am 03:56 AM

TooptimizeGoapplicationsinteractingwithPostgreSQLorMySQL,focusonindexing,selectivequeries,connectionhandling,caching,andORMefficiency.1)Useproperindexing—identifyfrequentlyqueriedcolumns,addindexesselectively,andusecompositeindexesformulti-columnquer

解釋說明 解釋說明 Jul 14, 2025 am 02:57 AM

defer的核心作用是推遲執行函數調用直到當前函數返回,常用於資源清理。具體包括:1.確保文件、網絡連接、鎖等資源及時釋放;2.執行順序為後進先出(LIFO),最後定義的defer最先執行;3.參數在defer定義時即確定,非執行時求值,若需捕獲變量變化可用閉包或指針;4.避免在循環中濫用defer,防止資源累積未及時釋放。

See all articles