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

推荐订阅源

The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
博客园 - 聂微东
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
LangChain Blog
WordPress大学
WordPress大学
H
Help Net Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
MyScale Blog
MyScale Blog
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
P
Proofpoint News Feed
D
DataBreaches.Net

博客园 - 干炸小黄鱼

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
}