今天看了下labrpc模块,学习了相关的golang语法。总结下
map
interface{},空的可以表示任何类型,类似java的object
struct
new 初始化零值
make map slice channel
func MakeNetwork() *Network { rn := &Network{} rn.reliable = true rn.ends = map[interface{}]*ClientEnd{} rn.enabled = map[interface{}]bool{} rn.servers = map[interface{}]*Server{} rn.connections = map[interface{}](interface{}){} return rn }
上面的函数指定了很多初始化方法
func (rn *Network) ReadEndnameInfo(endname interface{}) (bool, interface{}, *Server, bool) { rn.mu.Lock() defer rn.mu.Unlock() enabled := rn.enabled[endname] servername := rn.connections[endname] var server *Server if servername != nil { server = rn.servers[servername] } reliable := rn.reliable return enabled, servername, server, reliable }
interface{}表示任何类型
func (rn *Network) IsServerDead(endname interface{}, servername interface{}, server *Server) bool { rn.mu.Lock() defer rn.mu.Unlock() if rn.enabled[endname] == false || rn.servers[servername] != server { return true } return false }
定义struct的方法
有疑问加站长微信联系(非本文作者)