package main import ( context "context" ) // NameCode 省份地区的结构 type NameCode struct { Name string Code string Child []*NameCode } var province = &KeyList{} type provinceserver struct { } // Province 省份 与 代码 func (ps *provinceserver) Province(ctx context.Context, req *Request) (*NameCodeReply, error) { reply := &NameCodeReply{} nc := GetRandomKeyBySlice(province.GetKeys()).(NameCode) reply.Name = nc.Name reply.Code = nc.Code return reply, nil } // Area 区(市, 悬) 与 代码 func (ps *provinceserver) Area(ctx context.Context, req *Request) (*NameCodeReply, error) { reply := &NameCodeReply{} nc := GetRandomKeyBySlice(province.GetKeys()).(NameCode) areanc := GetRandomKeyBySlice(nc.Child).(*NameCode) reply.Name = areanc.Name reply.Code = areanc.Code return reply, nil } // City 城市 与 代码 func (ps *provinceserver) City(ctx context.Context, req *Request) (*NameCodeReply, error) { reply := &NameCodeReply{} nc := GetRandomKeyBySlice(province.GetKeys()).(NameCode) areanc := GetRandomKeyBySlice(nc.Child).(*NameCode) citync := GetRandomKeyBySlice(areanc.Child).(*NameCode) reply.Name = citync.Name reply.Code = citync.Code return reply, nil } // AreaParent 区域 与 代码 与 父类一起 返回 func (ps *provinceserver) AreaParent(ctx context.Context, req *Request) (*NameCodeParentReply, error) { reply := &NameCodeParentReply{} nc := GetRandomKeyBySlice(province.GetKeys()).(NameCode) parentReply := &NameCodeParentReply{} parentReply.Name = nc.Name parentReply.Code = nc.Code areanc := GetRandomKeyBySlice(nc.Child).(*NameCode) reply.Name = areanc.Name reply.Code = areanc.Code reply.Parent = parentReply return reply, nil } // CityParent 同上 多级返回 func (ps *provinceserver) CityParent(ctx context.Context, req *Request) (*NameCodeParentReply, error) { reply := &NameCodeParentReply{} nc := GetRandomKeyBySlice(province.GetKeys()).(NameCode) parentReply := &NameCodeParentReply{} parentReply.Name = nc.Name parentReply.Code = nc.Code areanc := GetRandomKeyBySlice(nc.Child).(*NameCode) areaReply := &NameCodeParentReply{} areaReply.Name = areanc.Name areaReply.Code = areanc.Code areaReply.Parent = parentReply citync := GetRandomKeyBySlice(areanc.Child).(*NameCode) reply.Name = citync.Name reply.Code = citync.Code reply.Parent = areaReply return reply, nil }