Golang:存储单位转换判断

Posted by 启示录 Blog on May 1, 2016

这段代码是把存储单位转换通过移位运算得出需要的结果.源码来自于Google的开源项目zoekt.api.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
func (s Stats) HumanBytesLoaded() string {
	suffix := ""
	b := s.BytesLoaded
	if s.BytesLoaded > (1 << 30) {
		suffix = "G"
		b = s.BytesLoaded / (1 << 30)
	} else if s.BytesLoaded > (1 << 20) {
		suffix = "M"
		b = s.BytesLoaded / (1 << 20)
	} else if s.BytesLoaded > (1 << 10) {
		suffix = "K"
		b = s.BytesLoaded / (1 << 10)
	}

	return fmt.Sprintf("%d%s", b, suffix)
}

b := s.BytesLoaded的值是byte的单位:

  • 1GB=102410241024=1«30=1073741824
  • 1MB=1024*1024=1«20=1048576
  • 1KB=1«10=1024
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//测试代码
package main

import (
	"fmt"
	"os"
	"strconv"
)

func main() {
	var com string
	value,_:=strconv.ParseInt(os.Args[1],10,64)
	com = load(value)
	fmt.Printf("%s", com)
}
func  load(s int64) string {
	suffix := ""
	b := s
	if s > (1 << 30) {
		suffix = "G"
		b = s / (1 << 30)
	} else if s > (1 << 20) {
		suffix = "M"
		b = s / (1 << 20)
	} else if s > (1 << 10) {
		suffix = "K"
		b = s / (1 << 10)
	}
	return fmt.Sprintf("%d%s\n", b, suffix)
}
1
2
3
//运行测试
➜ /home/sec/go/src/main >go run byte.go 10000000
9M

参考资料:

Google zoekt source