如何解决这个简单情况下的泛型缺失问题?

huangapple go评论66阅读模式
英文:

How to go around the lack of generics in this simple case

问题

我正在创建一个简单的sql映射器,允许我在运行时创建sql映射,因为我不知道数据库模式会是什么样子。

考虑以下结构:

type SqlColumn struct {
    name string
    columnType ? //int float等...
}

对于columnType字段,我应该使用什么类型?

我能想到的唯一方法是使用字符串或常量,并使用反射处理其余部分,我走对了吗?

英文:

I am creating a simple sql mapper that allows me to create sql mappings at run time because i have no idea how the database schema will be like.

consider the following struct:

type SqlColumn struct {
	name string
	columnType ? //int float etc...
}

what type should i use for the columnType field?

the only way i could think of is to use strings or a const and handle the rest using reflection, am i on the right path?

答案1

得分: 3

使用interface{}来实现:

type SqlColumn struct {
    name       string
    columnType interface{}
}
英文:

Use interface{} for this:

type SqlColumn struct {
    name string
    columnType interface{}
}

huangapple
  • 本文由 发表于 2014年7月13日 22:02:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/24723454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定