diff --git a/kadai1/convert/conver.go b/kadai1/convert/conver.go new file mode 100644 index 00000000..943470c4 --- /dev/null +++ b/kadai1/convert/conver.go @@ -0,0 +1,75 @@ +package convert + +import ( + "fmt" + "image" + "image/gif" + _ "image/jpeg" + "image/png" + "io/ioutil" + "os" + "path/filepath" +) + +//再帰的にファイルを読み込む +func GetAllFile(pathname string, ipt []string) ([]string, error) { + rd, err := ioutil.ReadDir(pathname) + if err != nil { + fmt.Println("Failed to read dir:", err) + return ipt, err + } + for _, fi := range rd { + if fi.IsDir() { + fullDir := pathname + "/" + fi.Name() + ipt, err = GetAllFile(fullDir, ipt) + if err != nil { + fmt.Println("Failed to read dir:", err) + return ipt, err + } + } else { + fullName := pathname + "/" + fi.Name() + ipt = append(ipt, fullName) + } + } + return ipt, nil +} + +//画像の形式を変換する +func Conv(ipt, opt string) (s string, err error) { + s1 := "right" + file, err := os.Open(ipt) + if err != nil { + return s1, err + } + //assert(err, "Invalid image file path ") + defer file.Close() + + img, _, err := image.Decode(file) + if err != nil { + return s1, err + } + //assert(err, "Failed to convert file to image.") + + out, err := os.Create(opt) + if err != nil { + return s1, err + } + defer out.Close() + + switch filepath.Ext(opt) { + case ".png": + err = png.Encode(out, img) + case ".gif": + err = gif.Encode(out, img, nil) + } + if err != nil { + return s1, err + } + return s1, nil +} + +//func assert(err error, msg string) { +// if err != nil { +// panic(err.Error() + ":" + msg) +// } +//} diff --git a/kadai1/convert/conver_test.go b/kadai1/convert/conver_test.go new file mode 100644 index 00000000..c3dc11f4 --- /dev/null +++ b/kadai1/convert/conver_test.go @@ -0,0 +1,47 @@ +package convert + +import ( + "convert" + "path/filepath" + "testing" +) + +func TestAllfile(t *testing.T) { + t.Helper() + var s []string + output, _ := GetAllFile("./img", s) + if len(output) != 15 { + t.Errorf("Image file does not complete readed,expected 15,but %v ", len(output)) //画像の枚数が違う + } +} + +func TestTableAllfile(t *testing.T) { + t.Helper() + var s []string + var tests = []struct { + pathinput string + expect int + }{ + {"./img/img_test1", 3}, + {"./img/img_test2", 4}, + {"./img/img_test3", 5}, + } + for _, te := range tests { + output, _ := GetAllFile(te.pathinput, s) + if len(output) != te.expect { + t.Errorf("Image file does not complete readed,expected %v,but %v ", tests[1], len(output)) //画像の枚数が違う + } + } +} + +func TestConv(t *testing.T) { + t.Helper() + err := convert.Conv("./img/1.0.jpg", "./img/1.0.png") + if err != nil { + t.Errorf("error happened %v", err) + } + matches, _ := filepath.Glob("./img/*.png") + if matches == nil { + t.Errorf("Image file does not converted %v", matches) + } +} diff --git a/kadai1/convert/img/1.0.jpg b/kadai1/convert/img/1.0.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/convert/img/1.0.jpg differ diff --git a/kadai1/convert/img/1.1.jpg b/kadai1/convert/img/1.1.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/convert/img/1.1.jpg differ diff --git a/kadai1/convert/img/1.2.jpg b/kadai1/convert/img/1.2.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/convert/img/1.2.jpg differ diff --git a/kadai1/convert/img/img_test1/1.jpg b/kadai1/convert/img/img_test1/1.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/convert/img/img_test1/1.jpg differ diff --git a/kadai1/convert/img/img_test1/2.jpg b/kadai1/convert/img/img_test1/2.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/convert/img/img_test1/2.jpg differ diff --git a/kadai1/convert/img/img_test1/3.jpg b/kadai1/convert/img/img_test1/3.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/convert/img/img_test1/3.jpg differ diff --git a/kadai1/convert/img/img_test2/1.jpg b/kadai1/convert/img/img_test2/1.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/convert/img/img_test2/1.jpg differ diff --git a/kadai1/convert/img/img_test2/2.jpg b/kadai1/convert/img/img_test2/2.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/convert/img/img_test2/2.jpg differ diff --git a/kadai1/convert/img/img_test2/3.jpg b/kadai1/convert/img/img_test2/3.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/convert/img/img_test2/3.jpg differ diff --git a/kadai1/convert/img/img_test2/4.jpg b/kadai1/convert/img/img_test2/4.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/convert/img/img_test2/4.jpg differ diff --git a/kadai1/convert/img/img_test3/1.jpg b/kadai1/convert/img/img_test3/1.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/convert/img/img_test3/1.jpg differ diff --git a/kadai1/convert/img/img_test3/2.jpg b/kadai1/convert/img/img_test3/2.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/convert/img/img_test3/2.jpg differ diff --git a/kadai1/convert/img/img_test3/3.jpg b/kadai1/convert/img/img_test3/3.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/convert/img/img_test3/3.jpg differ diff --git a/kadai1/convert/img/img_test3/4.jpg b/kadai1/convert/img/img_test3/4.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/convert/img/img_test3/4.jpg differ diff --git a/kadai1/convert/img/img_test3/5.jpg b/kadai1/convert/img/img_test3/5.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/convert/img/img_test3/5.jpg differ diff --git a/kadai1/go.mod b/kadai1/go.mod new file mode 100644 index 00000000..ea118c73 --- /dev/null +++ b/kadai1/go.mod @@ -0,0 +1,3 @@ +module image.go + +go 1.16 diff --git a/kadai1/img/1.0.jpg b/kadai1/img/1.0.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/img/1.0.jpg differ diff --git a/kadai1/img/1.1.jpg b/kadai1/img/1.1.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/img/1.1.jpg differ diff --git a/kadai1/img/1.2.jpg b/kadai1/img/1.2.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/img/1.2.jpg differ diff --git a/kadai1/img/img_test/1.jpg b/kadai1/img/img_test/1.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/img/img_test/1.jpg differ diff --git a/kadai1/img/img_test/2.jpg b/kadai1/img/img_test/2.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/img/img_test/2.jpg differ diff --git a/kadai1/img/img_test/3.jpg b/kadai1/img/img_test/3.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai1/img/img_test/3.jpg differ diff --git a/kadai1/main.go b/kadai1/main.go new file mode 100644 index 00000000..9219dbb9 --- /dev/null +++ b/kadai1/main.go @@ -0,0 +1,39 @@ +/*This is a training code for learning Go language. +And this code can change the format of images as you like*/ +package main + +import ( + "bufio" + "convert" + "fmt" + "os" + "strings" +) + +type oriPath struct { + inputPA string + outputPA string +} + +func main() { + var a oriPath + a.inputPA = "./img" + a.outputPA = "./img" + scanner := bufio.NewScanner(os.Stdin) + fmt.Print("Input the image format you want(like png or gif):") + if !scanner.Scan() { + fmt.Println("Please input the format.") + return + } + srcPath := scanner.Text() + + var s []string + s, _ = convert.GetAllFile(a.inputPA, s) + for _, i := range s { + j := strings.Replace(i, "jpg", srcPath, -1) //jpgから任意の形式へ変換する + err := convert.Conv(i, j) + if err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err.Error()) + } + } +} diff --git a/kadai2/convert/conver.go b/kadai2/convert/conver.go new file mode 100644 index 00000000..943470c4 --- /dev/null +++ b/kadai2/convert/conver.go @@ -0,0 +1,75 @@ +package convert + +import ( + "fmt" + "image" + "image/gif" + _ "image/jpeg" + "image/png" + "io/ioutil" + "os" + "path/filepath" +) + +//再帰的にファイルを読み込む +func GetAllFile(pathname string, ipt []string) ([]string, error) { + rd, err := ioutil.ReadDir(pathname) + if err != nil { + fmt.Println("Failed to read dir:", err) + return ipt, err + } + for _, fi := range rd { + if fi.IsDir() { + fullDir := pathname + "/" + fi.Name() + ipt, err = GetAllFile(fullDir, ipt) + if err != nil { + fmt.Println("Failed to read dir:", err) + return ipt, err + } + } else { + fullName := pathname + "/" + fi.Name() + ipt = append(ipt, fullName) + } + } + return ipt, nil +} + +//画像の形式を変換する +func Conv(ipt, opt string) (s string, err error) { + s1 := "right" + file, err := os.Open(ipt) + if err != nil { + return s1, err + } + //assert(err, "Invalid image file path ") + defer file.Close() + + img, _, err := image.Decode(file) + if err != nil { + return s1, err + } + //assert(err, "Failed to convert file to image.") + + out, err := os.Create(opt) + if err != nil { + return s1, err + } + defer out.Close() + + switch filepath.Ext(opt) { + case ".png": + err = png.Encode(out, img) + case ".gif": + err = gif.Encode(out, img, nil) + } + if err != nil { + return s1, err + } + return s1, nil +} + +//func assert(err error, msg string) { +// if err != nil { +// panic(err.Error() + ":" + msg) +// } +//} diff --git a/kadai2/convert/conver_test.go b/kadai2/convert/conver_test.go new file mode 100644 index 00000000..c3dc11f4 --- /dev/null +++ b/kadai2/convert/conver_test.go @@ -0,0 +1,47 @@ +package convert + +import ( + "convert" + "path/filepath" + "testing" +) + +func TestAllfile(t *testing.T) { + t.Helper() + var s []string + output, _ := GetAllFile("./img", s) + if len(output) != 15 { + t.Errorf("Image file does not complete readed,expected 15,but %v ", len(output)) //画像の枚数が違う + } +} + +func TestTableAllfile(t *testing.T) { + t.Helper() + var s []string + var tests = []struct { + pathinput string + expect int + }{ + {"./img/img_test1", 3}, + {"./img/img_test2", 4}, + {"./img/img_test3", 5}, + } + for _, te := range tests { + output, _ := GetAllFile(te.pathinput, s) + if len(output) != te.expect { + t.Errorf("Image file does not complete readed,expected %v,but %v ", tests[1], len(output)) //画像の枚数が違う + } + } +} + +func TestConv(t *testing.T) { + t.Helper() + err := convert.Conv("./img/1.0.jpg", "./img/1.0.png") + if err != nil { + t.Errorf("error happened %v", err) + } + matches, _ := filepath.Glob("./img/*.png") + if matches == nil { + t.Errorf("Image file does not converted %v", matches) + } +} diff --git a/kadai2/convert/img/1.0.jpg b/kadai2/convert/img/1.0.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/convert/img/1.0.jpg differ diff --git a/kadai2/convert/img/1.1.jpg b/kadai2/convert/img/1.1.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/convert/img/1.1.jpg differ diff --git a/kadai2/convert/img/1.2.jpg b/kadai2/convert/img/1.2.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/convert/img/1.2.jpg differ diff --git a/kadai2/convert/img/img_test1/1.jpg b/kadai2/convert/img/img_test1/1.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/convert/img/img_test1/1.jpg differ diff --git a/kadai2/convert/img/img_test1/2.jpg b/kadai2/convert/img/img_test1/2.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/convert/img/img_test1/2.jpg differ diff --git a/kadai2/convert/img/img_test1/3.jpg b/kadai2/convert/img/img_test1/3.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/convert/img/img_test1/3.jpg differ diff --git a/kadai2/convert/img/img_test2/1.jpg b/kadai2/convert/img/img_test2/1.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/convert/img/img_test2/1.jpg differ diff --git a/kadai2/convert/img/img_test2/2.jpg b/kadai2/convert/img/img_test2/2.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/convert/img/img_test2/2.jpg differ diff --git a/kadai2/convert/img/img_test2/3.jpg b/kadai2/convert/img/img_test2/3.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/convert/img/img_test2/3.jpg differ diff --git a/kadai2/convert/img/img_test2/4.jpg b/kadai2/convert/img/img_test2/4.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/convert/img/img_test2/4.jpg differ diff --git a/kadai2/convert/img/img_test3/1.jpg b/kadai2/convert/img/img_test3/1.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/convert/img/img_test3/1.jpg differ diff --git a/kadai2/convert/img/img_test3/2.jpg b/kadai2/convert/img/img_test3/2.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/convert/img/img_test3/2.jpg differ diff --git a/kadai2/convert/img/img_test3/3.jpg b/kadai2/convert/img/img_test3/3.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/convert/img/img_test3/3.jpg differ diff --git a/kadai2/convert/img/img_test3/4.jpg b/kadai2/convert/img/img_test3/4.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/convert/img/img_test3/4.jpg differ diff --git a/kadai2/convert/img/img_test3/5.jpg b/kadai2/convert/img/img_test3/5.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/convert/img/img_test3/5.jpg differ diff --git a/kadai2/go.mod b/kadai2/go.mod new file mode 100644 index 00000000..ea118c73 --- /dev/null +++ b/kadai2/go.mod @@ -0,0 +1,3 @@ +module image.go + +go 1.16 diff --git a/kadai2/img/1.0.jpg b/kadai2/img/1.0.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/img/1.0.jpg differ diff --git a/kadai2/img/1.1.jpg b/kadai2/img/1.1.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/img/1.1.jpg differ diff --git a/kadai2/img/1.2.jpg b/kadai2/img/1.2.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/img/1.2.jpg differ diff --git a/kadai2/img/img_test/1.jpg b/kadai2/img/img_test/1.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/img/img_test/1.jpg differ diff --git a/kadai2/img/img_test/2.jpg b/kadai2/img/img_test/2.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/img/img_test/2.jpg differ diff --git a/kadai2/img/img_test/3.jpg b/kadai2/img/img_test/3.jpg new file mode 100644 index 00000000..b168a75b Binary files /dev/null and b/kadai2/img/img_test/3.jpg differ diff --git a/kadai2/main.go b/kadai2/main.go new file mode 100644 index 00000000..9219dbb9 --- /dev/null +++ b/kadai2/main.go @@ -0,0 +1,39 @@ +/*This is a training code for learning Go language. +And this code can change the format of images as you like*/ +package main + +import ( + "bufio" + "convert" + "fmt" + "os" + "strings" +) + +type oriPath struct { + inputPA string + outputPA string +} + +func main() { + var a oriPath + a.inputPA = "./img" + a.outputPA = "./img" + scanner := bufio.NewScanner(os.Stdin) + fmt.Print("Input the image format you want(like png or gif):") + if !scanner.Scan() { + fmt.Println("Please input the format.") + return + } + srcPath := scanner.Text() + + var s []string + s, _ = convert.GetAllFile(a.inputPA, s) + for _, i := range s { + j := strings.Replace(i, "jpg", srcPath, -1) //jpgから任意の形式へ変換する + err := convert.Conv(i, j) + if err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err.Error()) + } + } +} diff --git a/kadai3-1/go.mod b/kadai3-1/go.mod new file mode 100644 index 00000000..304c88f7 --- /dev/null +++ b/kadai3-1/go.mod @@ -0,0 +1,3 @@ +module main.go + +go 1.16 diff --git a/kadai3-1/main.go b/kadai3-1/main.go new file mode 100644 index 00000000..df7f072b --- /dev/null +++ b/kadai3-1/main.go @@ -0,0 +1,62 @@ +package main + +import ( + "bufio" + "flag" + "fmt" + "io" + "math/rand" + "os" + "time" +) + +var t int + +//制限時間をオプションにする +func init() { + flag.IntVar(&t, "t", 20, "制限時間s") + flag.Parse() +} + +func main() { + fmt.Println("タイピングゲームが始まった!制限時間", t, "s") + num, score := 0, 0 + timeout := time.After(time.Second * time.Duration(t)) + for sign := true; sign == true; { + word := RandomWord() + num++ + fmt.Println("単語NO:", num, "この英単語をタイピングしてください:", word) + c := imp(os.Stdin) + select { + case right := <-c: + if right == word { + fmt.Println("正解です!") + score++ + } else { + fmt.Println("残念、不正解です。") + } + case <-timeout: + fmt.Println("時間です!") + sign = false + } + } + fmt.Println("ゲーム終了! やった単語数", num, " 時間内に正確単語数:", score) +} + +//英単語をランダムに取り出す +func RandomWord() (word string) { + words := []string{"banana", "apple", "milk", "fruits", "cat", "car", "elephant", "unbralla", "interface", "tissues", "format"} + rand.Seed(time.Now().Unix()) + num := rand.Intn(len(words)) + return words[num] +} + +//入力単語を取得する +func imp(r io.Reader) <-chan string { + wordCh := make(chan string, 1) + scanner := bufio.NewScanner(r) + scanner.Scan() + wordCh <- scanner.Text() + //close(wordCh) + return wordCh +} diff --git a/kadai3-1/main_test.go b/kadai3-1/main_test.go new file mode 100644 index 00000000..e33aa9c2 --- /dev/null +++ b/kadai3-1/main_test.go @@ -0,0 +1,12 @@ +package main + +import ( + "testing" +) + +func TestRandomWord(t *testing.T) { + word := RandomWord() + if word == nil { + t.Error("文字取り出さない") + } +} diff --git a/kadai3-2/download.go b/kadai3-2/download.go new file mode 100644 index 00000000..735324fc --- /dev/null +++ b/kadai3-2/download.go @@ -0,0 +1,150 @@ +package main + +import ( + "context" + "flag" + "fmt" + "io/ioutil" + "log" + "net/http" + "os" + "strconv" + "sync" + "time" +) + +const size = 4 //分割の数 +var muex sync.Mutex +var wg = sync.WaitGroup{} +var t int +var url, fileph, dpath, spath string + +func init() { + flag.IntVar(&t, "t", 10, "制限時間m") + flag.StringVar(&dpath, "p", "https://www.robots.ox.ac.uk/~vgg/data/paintings/painting_dataset_2014.xlsx", "ダウンローダアドレス") + flag.StringVar(&spath, "s", "./painting_dataset_2014.xlsx", "保存アドレスとname") + flag.Parse() +} + +func main() { + fmt.Println("ダウンロードスタート...制限時間", t, "m") + url = dpath + fileph = spath + datetime := time.Now() + fmt.Println(datetime) + filelen := Getfilelen() + fmt.Println("ファイルサイズ:", calculatelen(filelen)) + ctx, cancel := context.WithTimeout(context.Background(), time.Duration(t)*time.Minute) + file, err := os.Create(fileph) + if err != nil { + panic(err) + } + part := filelen/size + 1 //startとendを指定する + work(url, file, filelen, part, ctx) + defer file.Close() + cancel() + fmt.Println("ダウンロード終了。") + datetime2 := time.Now() + fmt.Println(datetime2) +} + +func work(url string, file *os.File, filelen int, part int, ctx context.Context) { + ch := make(chan int, 1) + for j := 0; j < size; j++ { + ch <- j + select { + case <-ctx.Done(): + fmt.Println("時間を超え、ダウンロード停止") + return + case <-ch: + Download(url, file, filelen, j, part) + } + } + close(ch) +} + +//ダウンロード機能 +func Download(url string, file *os.File, filelen int, i int, part int) { + var start, end int + fmt.Println(strconv.Itoa(i) + "番目の部分をダウンロードスタート") + muex.Lock() + client := &http.Client{} + req, err := http.NewRequest("GET", url, nil) + + if i != 0 { //rangeのstartとendを指定 + start = i*part + 1 + } else { + start = 0 + } + if i+1 == size { + end = filelen + } else { + end = (i + 1) * part + } + + str := strconv.FormatInt(int64(start), 10) + "-" + strconv.FormatInt(int64(end), 10) + fmt.Println("Range"+strconv.Itoa(i), "byte="+str) + req.Header.Set("Range", "bytes="+str) + resp, err := client.Do(req) + if err != nil { + log.Printf("client.Do err:%v", err) + } + + fmt.Println(strconv.Itoa(i)+"番目のダウンロードの長さ:", resp.Header.Get("Content-Length")) + op, err := file.Seek(int64(start), 0) + fmt.Println("今までダウンロードの長さ:", op) + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + log.Printf("read file error: %v", err) + } + if len(body) == 0 { + fmt.Println("no file") + } + len, err := file.Write(body) + fmt.Println(strconv.Itoa(i)+"番目のファイルを書き込み、長さ:", len) + resp.Body.Close() + muex.Unlock() +} + +//ファイルの長さを取得 +func Getfilelen() (filelen int) { + resp, err := http.Get(string(url)) + if err != nil { + panic(err) + } + + filelenStr, ext := resp.Header["Content-Length"] + if !ext || len(filelenStr) == 0 { + filelenStr = []string{"0"} + } + + filelen, fileerr := strconv.Atoi(filelenStr[0]) + fmt.Println("ファイルの長さ:", filelen) + if fileerr != nil { + fmt.Println(fileerr) + filelen = 0 + fmt.Println("ファイルサイズ未知") + } + return filelen +} + +//ファイルサイズを計算する +func calculatelen(len int) string { + if len < 1024 { + return fmt.Sprintf("%d Btye", len) + } + kb := float32(len) / 1024 + if kb < 1024 { + return fmt.Sprintf("%f Kb", kb) + } + mb := kb / 1024 + if mb < 1024 { + return fmt.Sprintf("%f Mb", mb) + } + gb := mb / 1024 + if mb < 1024 { + return fmt.Sprintf("%f GB", gb) + } + return fmt.Sprintf("%f PB", gb/1024) +}