求问下面的Add2这里怎么实现?sum为interface好像没法任意求和?funcAdd(vars...int)(retint){varsumintfor_,v:=rangevars{//fmt.Println("v",v)sum+=v}returnsum}//这里怎么实现?sum为interface好像没法任意求和?funcAdd2(vars...interface{})(retinterface{}){/*varsuminterface{}for_,v:=rangevars{switchv.(type){caseint:sum+=vcasefloat64:sum+=vcasestring:sum+=v}}*/returnsum}funcmain(){fmt.Println(Add2(1,2))fmt.Println(Add2(1,2,3))fmt.Println(Add2(2.4,3.6,1.0))fmt.Println(Add2("Hello","world","zhong"))}
哈士奇WWW
相关分类