随笔 - 56  文章 - 0 评论 - 0 阅读 - 50888

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
 
import (
    "errors"
    "fmt"
)
 
func main() {
 
    if x, e := DOCFactory("XXX"); e == nil {
        x.Summary()
    } else {
        fmt.Println(e.Error())
    }
}
 
type Article struct{ Topic string }
 
type News struct{ Title string }
 
type Doc interface{ Summary() }
 
func (a Article) Summary() {
    fmt.Printf("Article topic is %s", a.Topic)
}
 
func (n News) Summary() {
    fmt.Printf("News title is %s", n.Title)
}
 
func DOCFactory(t string) (Doc, error) {
    switch t {
    case "Article":
        return Article{"aaa"}, nil
    case "News":
        return News{"bbb"}, nil
    default:
        return nil, errors.New("no such struct")
    }
}
posted on   scala  阅读(401)  评论(0)    收藏  举报
编辑推荐:
· MySQL索引完全指南:让你的查询速度飞起来
· 一个字符串替换引发的性能血案:正则回溯与救赎之路
· 为什么说方法的参数最好不要超过4个?
· C#.Net 筑基-优雅 LINQ 的查询艺术
· 一个自认为理想主义者的程序员,写了5年公众号、博客的初衷
阅读排行:
· 我用这13个工具,让开发效率提升了5倍!
· 一次 .NET 性能优化之旅:将 GC 压力降低 99%
· Coze工作流实战:一键生成鸡汤视频——厉害的人,早已戒掉情绪
· 商品中心—15.库存分桶扣减的技术文档
· C++服务开发环境-万事开头难
点击右上角即可分享
微信分享提示