go iota的间隔相加,5和1024倍

1, 间隔5的

package main

import (
	"fmt"
	"reflect"
)

type Author struct {
	Name         int      `json:Name`
	Publications []string `json:Publication,omitempty`
}

func main() {
	t := reflect.TypeOf(Author{})
	for i := 0; i < t.NumField(); i++ {
		name := t.Field(i).Name
		s, _ := t.FieldByName(name)
		fmt.Println(">>1", name, ">>2", s.Tag)
	}
	const (
		B = 0 + (5 * iota)
		KiB
		MiB
		GiB
		TiB
		PiB
		EiB
	)
	fmt.Println("B>>", B)
	fmt.Println("KiB>>", KiB)
	fmt.Println("MiB>>", MiB)
	fmt.Println("GiB>>", GiB)
	fmt.Println("TiB>>", TiB)
	fmt.Println("PiB>>", PiB)
	fmt.Println("EiB>>", EiB)
}

输出:

>>1 Name >>2 json:Name
>>1 Publications >>2 json:Publication,omitempty
B>> 0
KiB>> 5
MiB>> 10
GiB>> 15
TiB>> 20
PiB>> 25
EiB>> 30

2, 1024倍的

package main

import (
	"fmt"
)

func main() {
	const (
		B = 1 << (10 * iota)
		KiB
		MiB
		GiB
		TiB
		PiB
		EiB
	)
	fmt.Println("B>>", B)
	fmt.Println("KiB>>", KiB)
	fmt.Println("MiB>>", MiB)
	fmt.Println("GiB>>", GiB)
	fmt.Println("TiB>>", TiB)
	fmt.Println("PiB>>", PiB)
	fmt.Println("EiB>>", EiB)
}

输出:

B>> 1
KiB>> 1024
MiB>> 1048576
GiB>> 1073741824
TiB>> 1099511627776
PiB>> 1125899906842624
EiB>> 1152921504606846976
posted @ 2023-03-10 16:53  ty1539  阅读(21)  评论(0编辑  收藏  举报