使用通用函数重置缓存。

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

Using generic function to reset cache

问题

我刚刚开始在一个项目中使用泛型。我在go playground中设置了以下示例。我试图创建一个通用函数来重置两种不同类型的缓存,它们具有相同的字段。此外,我无法弄清楚为什么无法在通用函数中访问这些字段。

错误信息如下:

  1. ./prog.go:21:4: c.Count undefined (type *C is pointer to type parameter, not type parameter)
  2. ./prog.go:22:4: c.List undefined (type *C is pointer to type parameter, not type parameter)

感谢任何帮助。

以下是翻译好的代码:

  1. package main
  2. import "fmt"
  3. type CacheOne struct {
  4. Count int
  5. List []string
  6. }
  7. type CacheTwo struct {
  8. Count int
  9. List []string
  10. }
  11. type cachable interface {
  12. CacheOne | CacheTwo
  13. }
  14. // resetSCache resets the Count & List of a given cachable type.
  15. func resetCache[C cachable](c *C) {
  16. c.Count = 0
  17. c.List = []string{}
  18. }
  19. func main() {
  20. // Example usage
  21. dOne := &CacheOne{
  22. Count: 5,
  23. List: []string{"hello", "world"},
  24. }
  25. dTwo := &CacheTwo{
  26. Count: 5,
  27. List: []string{"hello", "world"},
  28. }
  29. fmt.Println("重置之前...应该包含数据")
  30. fmt.Println(dOne)
  31. fmt.Println(dTwo)
  32. resetCache(dOne)
  33. resetCache(dTwo)
  34. fmt.Println("重置之后...应该重置为零值")
  35. fmt.Println(dOne)
  36. fmt.Println(dTwo)
  37. }
英文:

I'm just now starting to work with generics for a project. I've set up the following example in a go playground. I'm trying to create a generic function to reset the cache of 2 different types, that have the same fields. Furthermore, I can't figure out why I can't access those fields withing the generic function.

The error is:

  1. ./prog.go:21:4: c.Count undefined (type *C is pointer to type parameter, not type parameter)
  2. ./prog.go:22:4: c.List undefined (type *C is pointer to type parameter, not type parameter)

Any help is appreciated.

https://go.dev/play/p/ojkNe1D-hSE

  1. package main
  2. import "fmt"
  3. type CacheOne struct {
  4. Count int
  5. List []string
  6. }
  7. type CacheTwo struct {
  8. Count int
  9. List []string
  10. }
  11. type cachable interface {
  12. CacheOne | CacheTwo
  13. }
  14. // resetSCache resets the Count & List of a given cachable type.
  15. func resetCache[C cachable](c *C) {
  16. c.Count = 0
  17. c.List = []string{}
  18. }
  19. func main() {
  20. // Example usage
  21. dOne := &CacheOne{
  22. Count: 5,
  23. List: []string{"hello", "world"},
  24. }
  25. dTwo := &CacheTwo{
  26. Count: 5,
  27. List: []string{"hello", "world"},
  28. }
  29. fmt.Println("Before reseting...whould be populated with data")
  30. fmt.Println(dOne)
  31. fmt.Println(dTwo)
  32. resetCache(dOne)
  33. resetCache(dTwo)
  34. fmt.Println("Before reseting...whould be reset with zero values")
  35. fmt.Println(dOne)
  36. fmt.Println(dTwo)
  37. }

答案1

得分: 1

你现在试图实现的功能在Go语言中目前是不允许的。你可以在这个答案的链接中找到更多详细信息(链接:https://stackoverflow.com/a/71531151/14394371)。也许将来会有所改变,但据我所知,你必须坚持以下实现方式:

  1. package main
  2. import "fmt"
  3. type CacheOne struct {
  4. Count int
  5. List []string
  6. }
  7. type CacheTwo struct {
  8. Count int
  9. List []string
  10. }
  11. type cachable interface {
  12. SetCount(count int)
  13. SetList([]string)
  14. }
  15. func (c *CacheOne) SetCount(count int) {
  16. c.Count = count
  17. }
  18. func (c *CacheOne) SetList(list []string) {
  19. c.List = list
  20. }
  21. func (c *CacheTwo) SetCount(count int) {
  22. c.Count = count
  23. }
  24. func (c *CacheTwo) SetList(list []string) {
  25. c.List = list
  26. }
  27. // resetSCache resets the Count & List of a given cachable type.
  28. func resetCache[C cachable](c C) {
  29. c.SetCount(0)
  30. c.SetList(make([]string, 0))
  31. }
  32. func main() {
  33. // Example usage
  34. dOne := &CacheOne{
  35. Count: 5,
  36. List: []string{"hello", "world"},
  37. }
  38. dTwo := &CacheTwo{
  39. Count: 5,
  40. List: []string{"hello", "world"},
  41. }
  42. fmt.Println("重置之前...应该包含数据")
  43. fmt.Println(dOne)
  44. fmt.Println(dTwo)
  45. resetCache(dOne)
  46. resetCache(dTwo)
  47. fmt.Println("重置之后...应该重置为零值")
  48. fmt.Println(dOne)
  49. fmt.Println(dTwo)
  50. }

如果你运行这段代码,你应该会得到期望的输出:

  1. 重置之前...应该包含数据
  2. &{5 [hello world]}
  3. &{5 [hello world]}
  4. 重置之后...应该重置为零值
  5. &{0 []}
  6. &{0 []}

我同意这种方法可能有点繁琐,但这就是它的实现方式 使用通用函数重置缓存。
如果你找到了更聪明的方法,请告诉我。
希望这能帮助你解决问题,谢谢!

英文:

what you're trying to achieve is not permitted by Go as of now. You can find more details in this answer link. Probably something could change in the future but AFAIK, you've to stick to the following implementation:

  1. package main
  2. import "fmt"
  3. type CacheOne struct {
  4. Count int
  5. List []string
  6. }
  7. type CacheTwo struct {
  8. Count int
  9. List []string
  10. }
  11. type cachable interface {
  12. SetCount(count int)
  13. SetList([]string)
  14. }
  15. func (c *CacheOne) SetCount(count int) {
  16. c.Count = count
  17. }
  18. func (c *CacheOne) SetList(list []string) {
  19. c.List = list
  20. }
  21. func (c *CacheTwo) SetCount(count int) {
  22. c.Count = count
  23. }
  24. func (c *CacheTwo) SetList(list []string) {
  25. c.List = list
  26. }
  27. // resetSCache resets the Count & List of a given cachable type.
  28. func resetCache[C cachable](c C) {
  29. c.SetCount(0)
  30. c.SetList(make([]string, 0))
  31. }
  32. func main() {
  33. // Example usage
  34. dOne := &CacheOne{
  35. Count: 5,
  36. List: []string{"hello", "world"},
  37. }
  38. dTwo := &CacheTwo{
  39. Count: 5,
  40. List: []string{"hello", "world"},
  41. }
  42. fmt.Println("Before reseting...whould be populated with data")
  43. fmt.Println(dOne)
  44. fmt.Println(dTwo)
  45. resetCache(dOne)
  46. resetCache(dTwo)
  47. fmt.Println("Before reseting...whould be reset with zero values")
  48. fmt.Println(dOne)
  49. fmt.Println(dTwo)
  50. }

If you run this code, you should get the desired output:

  1. Before reseting...whould be populated with data
  2. &{5 [hello world]}
  3. &{5 [hello world]}
  4. Before reseting...whould be reset with zero values
  5. &{0 []}
  6. &{0 []}

I agree that this method could be a little cumbersome but it's what is 使用通用函数重置缓存。
If you find a smarter way to achieve just let me know in case.
I hope that this helps you in solving your issue, thanks!

huangapple
  • 本文由 发表于 2023年6月9日 22:58:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76441410.html
匿名

发表评论

匿名网友

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

确定