golang 函数定义及其接口实例

前端之家收集整理的这篇文章主要介绍了golang 函数定义及其接口实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
                                                                                                     
/有返回值 且返回一个
func max(aint, bint)int {
if a >return a
}
 b
}
//有返回值且返回二个
 multi_ret(keystring) (bool)m := map[string]int{"ont": 1,0);">"two":2,0);">"three":3}
var errbool
 valint
val,192);"> = m[key]
 val,192);"> err
}
//多个参数,相当于传进取一个数组
 sum(nums ...fmt.Println(nums,0);">"")
total0
for _,192);"> numrange nums += num
fmt.Println(total)
}
//返回值为函数
 nextNum()func()i,192);"> j0,128);">1
 tmp i + j
 j,192);"> tmp
}
}
//递归返回整数
 fact(n n ==0 * fact(n-1)
}
//结构
typerectstructwidthfloat64
heightfloat64
}
//结构函数
 (r *rect) area()float64 r.width r.height
}
 perimeter()2 (r.height r.height)
}
circleradiusfloat64
}
 (ccircle) math.Pi c.radius c.radius
}
 c.radius
}
//接口  注意rect和circle均实现了shape接口
shapeinterfacearea()perimeter() interface_test()r rect{width: height:4}
c circle{radius:4.3}
s []shape{&r,192);"> &c}
 sh sfmt.Println(sh)
fmt.Println(sh.area())
fmt.Println(sh.perimeter())
//自定义错误
myErrorargerrMsgstring
}
 (emyError) Error()string fmt.Sprintf("%d-%s",192);"> e.arg,192);"> e.errMsg)
}
 error_test(argerror) arg < - errors.New("BadArguments,negtive")
}else256 &myError{arg,0);">toolarge"}
 arg,128);">nil
}
 CopyFile(dstNamestring,192);"> srcName (writtenint64,192);">src,192);"> os.Open(srcName)
 !=nilfmt.Println("openFailed")
defer src.Close()
dst,192);"> os.Create(dstName)
"Createreturn
 dst.Close()
 io.Copy(dst,192);"> src)
}




查看原文:http://www.zoues.com/2016/10/27/golang-%e5%87%bd%e6%95%b0%e5%ae%9a%e4%b9%89%e5%8f%8a%e5%85%b6%e6%8e%a5%e5%8f%a3%e5%ae%9e%e4%be%8b/
原文链接:https://www.f2er.com/go/189257.html

猜你在找的Go相关文章