packagemainimport("fmt""strings")funcgetName(params...interface{}){aa:=strings.Join([]string(params),'_')fmt.Println(aa)}funcmain(){getName("redis","100","master")}看上面的代码,我想把传入的参数params用"_"连接,但是将params转成[]string类型时失败了,请指教。假如假如getName("redis","100","master")改为getName("redis",100,"master")编译没错误,但是运行时报错panic:interfaceconversion:interfaceisint,notstring
2 回答
摇曳的蔷薇
TA贡献1793条经验 获得超6个赞
packagemainimport("fmt""strconv""strings")funcgetName(params...interface{}){varparamSlice[]stringfor_,param:=rangeparams{switchv:=param.(type){casestring:paramSlice=append(paramSlice,v)caseint:strV:=strconv.FormatInt(int64(v),10)paramSlice=append(paramSlice,strV)default:panic("paramstypenotsupported")}}res:=strings.Join(paramSlice,"_")fmt.Println(res)}funcmain(){getName("redis",100,"master")}
添加回答
举报
0/150
提交
取消