Your Question

golang 1.18 泛型beta版已出,请问何时发布 gorm 3.0

Expected answer

跟随golang1.18发布

Comment From: FlameMida

只能说期待了

Comment From: zhuying412

有任何消息吗?

Comment From: a631807682

Gorm uses a lot of reflection and uses different logic under different parameter types, it is not suitable for using templates. So the question is, how should we modify the API so that generics can bring benefits to users?

Comment From: helphi

eg.

before

var user User
db.First(&user)

after

user := db.First[User]()

Comment From: jinzhu

eg.

before

go var user User db.First(&user)

after

go user := db.First[User]()

But with the new style, you can't reuse a variable to reduce GC.

Comment From: helphi

我觉得这个就要看用户在使用过程中如何取舍了,泛型这么多年才出来官方本来也是在性能与便捷之间进行取舍,gorm支持泛型至少表示我们还是跟着主旋律走的噻 ...

Comment From: jinzhu

There is no doubt, we will support generics, but for the above change, I don't see much value, especially it is a breaking changes.

Comment From: helphi

我这里只是随便举的一个例子,表示gorm看能不能通过泛型让大家少写点代码,不能当真,最终肯定要根据实际情况来噻。

Comment From: zhuying412

Is there a roadmap or scheme about supporting generics?

Comment From: a631807682

I don't think generics are needed from the current api design, like database/sql and encoding/json. At the moment I haven't figured out how to use generics to make the new api more user-friendly, any suggestions?

Comment From: yafeng-Soong

大家可以看看我搞的泛型分页封装示例,风格类似MyBatis-plus,博客https://juejin.cn/post/7078279187471679518,代码https://github.com/yafeng-Soong/blog-example/tree/main/gorm_page_generic 希望能有所帮助

Hey Guys,I wrote a simple example using generic, the style looks like Java's MyBatis-plus. blog——https://juejin.cn/post/7078279187471679518, Code——https://github.com/yafeng-Soong/blog-example/tree/main/gorm_page_generic Hope to help you.

Comment From: a631807682

The reason why pagination can use generics is that they have the same logic. Take db.Find as an example, the model of each user cannot be the same, so we use reflection, of course we can convert everything into generics like this, but is it better?

// Find
func Find[T interface{}]() ([]T, error) {
    ...
}

users, err := Find[User]()

// Unmarshal
type GenericsJSon[T interface{}] struct {
}

func (g GenericsJSon[T]) Unmarshal(data []byte) (*T, error) {
    ...
}

user, err := GenericsJSon[User]{}.Unmarshal([]byte(`{"name":"abc"}`))

For me, there are some API that can use generics like Offset/Limit/Count..., but others need to be discussed.

Comment From: yafeng-Soong

The reason why pagination can use generics is that they have the same logic. Take db.Find as an example, the model of each user cannot be the same, so we use reflection, of course we can convert everything into generics like this, but is it better?

```go // Find func FindT interface{} ([]T, error) { ... }

users, err := FindUser

// Unmarshal type GenericsJSon[T interface{}] struct { }

func (g GenericsJSon[T]) Unmarshal(data []byte) (*T, error) { ... }

user, err := GenericsJSon[User]{}.Unmarshal([]byte({"name":"abc"})) ```

For me, there are some API that can use generics like Offset/Limit/Count..., but others need to be discussed.

Thanks for your reply! I got it.

Comment From: cheerego

https://github.com/cheerego/gorm-generics

看看这个

Comment From: khl-i

支持了泛型会有什么收益呐?我从你的例子上只看到调用书写方式有不同,没看到支持泛型后有什么收益!

eg.

before

go var user User db.First(&user)

after

go user := db.First[User]()