duanchifo2866 2017-01-13 21:45 采纳率: 100%
浏览 52
已采纳

在golang中定义接口时如何思考?

I've noticed how hard it is to let go of the OOP style programming I've used in Java and PHP over the last 10 years or so. I'm giving golang a go (pun intended) since a few weeks, but I'm trying to feel natural around the composition over inheritance principle golang has.

How would I define a useful interface to make sure all these structs can fulfill it? I've tried to come up with a useful example that does not involve dogs, humans or weird constructs of vehicles...

package main

type Store struct {
  name  string
  phone string
}

type HardwareStore struct{ Store }

type FoodStore struct{ Store }

type OnlineStore struct {
  Store
  url string
}

I think it might be because I base my thinking around their state/data and not their behaviour, I'm struggling a bit.

The obvious first choice, without an interface, would probably be this (simplified), which of course fails:

s := Store{}
h := HardwareStore{}
f := FoodStore{}
o := OnlineStore{}

stores := []Store{s, h, f, o}
  • 写回答

1条回答 默认 最新

  • dsf4s5787 2017-01-13 23:27
    关注

    I think below is what you're after. Sorry for ugly names, but I had to make them standout to make a point. And keep in mind there are no virtual functions in go, even though example below sort of simulates one.

    package main
    
    import "fmt"
    
    
    type StoreInterface interface {
        getName() string
        setName(string)
        getPhone() string
        setPhone(string)
    }
    
    type Store struct {
       name  string
       phone string
    }
    
    func (store *Store) getName() string {
       return store.name;
    }
    
    func (store *Store) setName(newName string){
       store.name = newName;
    }
    
    func (store *Store) getPhone() string {
       return store.phone;
    }
    
    func (store *Store) setPhone(newPhone string){
       store.phone = newPhone;
    }
    
    type HardwareStore struct{ Store }
    
    type FoodStore struct{ Store }
    
    type OnlineStore struct {
        Store
        url string
    }
    
    func (os *OnlineStore) getName() string{
        return fmt.Sprintf("%v(%v)", os.name, os.url)
    }
    
    func main() {
        s := Store{name:"s", phone:"111"}
        h := HardwareStore{Store{name:"h", phone:"222"}}
        f := FoodStore{Store{name:"f", phone:"333"}}
        o := OnlineStore{Store:Store{name:"o", phone:"444"}, url:"http://test.com"}
    
        fmt.Println ("Printout 1")
        stores := []*Store{&s, &h.Store, &f.Store, &o.Store}
        for _, s := range stores {
            fmt.Printf("\t%v: %v
    ", s.name, s.phone);
        }
    
        fmt.Println ("Printout 2")
        stores2 := []StoreInterface{&s, &h, &f, &o}
        for _, s := range stores2 {
            fmt.Printf("\t%v: %v
    ", s.getName(), s.getPhone());
        }
     }
    

    Still, it goes without saying that you need to change your mindset from inheritance to functions, data structures and behavior. It's hard to break old habits (I come from many years of OOP too). But when in Rome you should think like Romans do :o) Otherwise you gonna be missing on some potential of the language.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,