惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
IT之家
IT之家
博客园_首页
博客园 - 【当耐特】
V
V2EX
Apple Machine Learning Research
Apple Machine Learning Research
G
Google Developers Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
GbyAI
GbyAI
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
H
Help Net Security
T
Tailwind CSS Blog
B
Blog RSS Feed
Martin Fowler
Martin Fowler
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
博客园 - 叶小钗
雷峰网
雷峰网
量子位

博客园 - 干炸小黄鱼

timex 处理时间戳 go 雪花算法 golang每日一库--协程池库ants golang每日一库--json解析库gjson python高级编程-asyncio python高级编程-condition python高级编程-event python装饰器-自动重试 EAP系统 go实现实现 SECS/GEM 协议 设备通信协议 SECS go项目使用Jenkins进行CICD go操作ES mongo db聚合查询 go如何使用mongodb Apache ShardingSphere paxos and raft (分布式一致性算法) go使用zookeeper分布式锁以及和redis差异 go使用 seata 示例 Alibaba 分布式事务 Seata go中使用saga go中使用TCC示例 分布式事务TCC 熔断器 Hystrix OR Sentinel k8s下部署consul and etcd Consul OR Etcd 【力扣hot100】双指针-盛水最多的容器 【力扣hot100】滑动窗口-最小覆盖子串 shell脚本合集 分布式id生成器
gorm-gen
干炸小黄鱼 · 2026-06-04 · via 博客园 - 干炸小黄鱼

snowid_method.go

package gorm_gen

import (
	"snowf"
	"gorm.io/gorm"
)

// SnowIDMethod
// Deprecated: recommend to use SnowIDMethod2
type SnowIDMethod struct {
	ID int64 `gorm:"column:id;type:bigint(20);primaryKey;comment:唯一 ID (雪花)" json:"id"` // 唯一 ID (雪花)
}

// TableName
// Deprecated: recommend to use SnowIDMethod2.TableName
func (t SnowIDMethod) TableName() string {
	return "@@table"
}

// BeforeCreate 在创建记录前生成雪花 ID
// Deprecated: recommend to use SnowIDMethod2.BeforeCreate
func (t *SnowIDMethod) BeforeCreate(db *gorm.DB) error {
	if t.ID <= 0 {
		t.ID = snowf.GetIDInt64(t.TableName())
	}
	return nil
}

type SnowIDMethod2 struct {
	ID snowf.ID `gorm:"column:id;type:bigint(20);primaryKey;comment:唯一 ID (雪花)" json:"id"` // 唯一 ID (雪花)
}

// TableName 获取表名
func (t SnowIDMethod2) TableName() string {
	return "@@table"
}

// BeforeCreate 在创建记录前生成雪花 ID
func (t *SnowIDMethod2) BeforeCreate(db *gorm.DB) error {
	if t.ID <= 0 {
		t.ID = snowf.GetID(t.TableName())
	}
	return nil
}

table_methods.go

package gorm_gen

import (
	"fmt"
)

type CommonMethod struct {
}

// TableName 获取表名
func (t CommonMethod) TableName() string {
	return "@@table"
}

func (t CommonMethod) TableColumn(column string) string {
	if "" == column {
		return ""
	}

	return fmt.Sprintf("%s.%s", t.TableName(), column)
}

func (t CommonMethod) TableColumns(columns interface{}) []string {
	tableColumnNames := make([]string, 0)

	switch v := columns.(type) {
	case []string:
		for _, column := range v {
			tableColumnNames = append(tableColumnNames, fmt.Sprintf("%s.%s", t.TableName(), column))
		}
	case string:
		tableColumnNames = []string{fmt.Sprintf("%s.%s", t.TableName(), v)}
	}

	return tableColumnNames
}