[問題] Golang的interface

作者: Nt1 (用功點吧!)   2018-01-12 18:52:37
小弟剛從Python轉Golang,有些關念還不是很清楚,請大家指教一下。
我有一個 Interface,定義兩個 method 如下:
type Content interface{
GetContent(filters map[string]string) (Content, error)
GetContents(filters map[string]string) ([]*Content, int64, error)
}
然後實做 struct 如下
type MyContent struct {
TypeA blablabla
TypeB blablabla
}
func (t *MyContent) GetContent(filters map[string]string) (
item Content, err error) {
item = &MyContent{}
// do something, 這個function 可以work
}
# 這個function可以work,可以透過實做 interface 來達到我想要的功能
func (t *MyContent) GetContents(filters map[string]string) (
items []*Content, total int64, err error) {
err = nil
items = []&MyContent{}
#^^^^^^^^^^^^^^^^^^^^^ 死在這行
#cannot use []MyContent literal (type []MyContent) as type []*Content in
#assignment
但第二個function一直出現錯誤,請問我該怎麼解呢?感謝

Links booklink

Contact Us: admin [ a t ] ucptt.com