当我尝试将新方法添加到别名类型时,package main
import (
"fmt"
)
type Strings []string
func (ss Strings) Add(s string) {
ss = append(ss, s)
}
func main() {
ss := make(Strings, 0)
ss = append(ss, "haha", "h3h3")
fmt.Println(ss) // got [haha h3h3]
ss.Add("lala")
fmt.Println(ss) // also got [haha h3h3], and why ?
}
方法无效。
ss
为什么没有" lala"被附加到MyFrame
?