package
main
import (
"fmt"
"reflect"
)
//实现 try catch 例子
func Try(fun func(), handler func(interface{})) {
defer func() {
if err := recover(); err != nil {
handler(err)
}
}()
fun()
}
//func main() {
// Try(func() {
// panic("foo")
// }, func(e interface{}) {
// print("XXXX", e, "EEEE")
// })
//}
//字节转文件大小
func main() {
Try(func() { // 必须要先声明defer,否则不能捕获到panic异常
var Kilobyte int64 = 1024
var Megabyte int64 = 1024 * Kilobyte
var Gigabyte int64 = 1024 * Megabyte
var Terabyte int64 = 1024 * Gigabyte
//var TB int
//totalByes := 1024
var totalByes int64 = 1024
fmt.Printf("XXXX%d", Terabyte)
fmt.Println("type:", reflect.TypeOf(Terabyte))
fmt.Println("type:", reflect.TypeOf(totalByes))
//fmt.Printf("%d %d", totalByes, Terabyte)
var TB int64 = totalByes / Terabyte
var TB_rest int64 = totalByes % Terabyte
var GB int64 = TB_rest / Gigabyte
var GB_rest int64 = TB_rest % Gigabyte
var MB int64 = GB_rest / Megabyte
var MB_rest int64 = GB_rest % Megabyte
var KB int64 = MB_rest / Kilobyte
var B int64 = MB_rest % Kilobyte
fmt.Printf("%d TB, %d GB, %d MB, %d KB, %d B", TB, GB, MB, KB, B)
}, func(e interface{}) {
print("XXXX", e, "EEEE")
})
}