Skip to content

Commit

Permalink
Update 2024-1-24-go-generic-pointer-receiver.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ZingLix authored Feb 8, 2024
1 parent 6f0adfa commit e79efa7
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion _posts/post/2024/2024-1-24-go-generic-pointer-receiver.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,13 @@ func Create[T any, Ptr PersonPtr[T]](id int) *T {
stu := Create[Student, *Student](1)
```

通过这个 trick,虽然很丑,但我们终于实现了 Go 中的泛型与 pointer receiver 的共存...
这样调用真的很丑,但好在 Go 这回终于做了个人,通过类型的自动推导可以自动推导出第二个参数,所以调用时可以简化为

```go
stu := Create[Student](1)
```

这样调用看起来就和谐了许多(虽然背后的实现需要用些难懂的 trick,但我们至少终于实现了 Go 中的泛型与 pointer receiver 的共存...

## 总结

Expand Down

0 comments on commit e79efa7

Please sign in to comment.