Skip to content

Instantly share code, notes, and snippets.

@chai2010
Created October 16, 2014 01:41
Show Gist options
  • Select an option

  • Save chai2010/31826c953977dc0ef137 to your computer and use it in GitHub Desktop.

Select an option

Save chai2010/31826c953977dc0ef137 to your computer and use it in GitHub Desktop.
泛型+接口

发现一个针对图像的 泛型+接口 的模式: http://godoc.org/github.com/chai2010/webp#Image

优点:

  • 接口, 可扩展图像格式, 比如 RGB/RGB48
  • 泛型, m.Pix() 返回底层数组结构, 可优化性能
  • 结合, m.Pix() 采用 []byte 类型, 可在运行时混合 RGB/RGB48 不同类型

按照 C++ 的泛型思路, RGB/RGB48 的 m.Pix() 类型应该为 []uint8 和 []uint16, 这样的话 就不能统一到 Go的 interface 类型了.

当然, 针对 RGB48, 自己手工解码 []byte 为 []uint16 要稍繁琐一点. Go1.4 的 generate 应该可以提供一些帮助.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment