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

推荐订阅源

F
Fortinet All Blogs
有赞技术团队
有赞技术团队
量子位
N
Netflix TechBlog - Medium
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
Google DeepMind News
Google DeepMind News
aimingoo的专栏
aimingoo的专栏
GbyAI
GbyAI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
Martin Fowler
Martin Fowler
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
V
V2EX
IT之家
IT之家
L
LangChain Blog
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 干炸小黄鱼

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
}