Go language learning - three minutes to see through iota

Origin Enumeration

When a recent demand, there is a need to enumerate the scene, about 10 + enumerated types, you do not want to be like the definition of a switch as perfunctory written

const (
    SwitchOff = 0
    SwitchOn  = 1
)

It was not very delicate ~

 

I think of the iota, in-depth understanding of the next, this little thing seems a little something.

And then return to their own needs - enumeration. With iota, do not display the definition of a lot of value.

Unused iota version

const (
  ColorRed         = 0
  ColorOrange 		= 1
  ColorYellow 		= 2
  ColorGrassland 	= 3
  ColorCyan 		= 4
  ColorBlue 		= 5
  ColorPurple 		= 6
)

  

Use iota version

const (
  ColorRed 		= iota
  ColorOrange 		
  ColorYellow 		
  ColorGrassland 	
  ColorCyan 			
  ColorBlue 			
  ColorPurple 		
)

 

Effect of both is the same, each of the enumerated values ​​corresponding to the same. iota from zero, each row is incremented down. At first glance, iota also appears quite high.

 

At the same time display of power, iota also a place everyone criticized.

For example, at this time if you need to add a "gray" enum type, in the unused iota version inserted inside this enumeration no matter what position, you can define a specific value, such as correspondence 7.

In the use of iota version which if added after ColorPurple, the corresponding value is 7, there is no problem.

But if in a different location, it will break the original balance, such as on ColorGrassland

const (
  ColorRed 		= iota		// 0
  ColorOrange 				// 1
  ColorYellow 				// 2
  ColorGrassland 			// 3
  ColorGray				// 4
  ColorCyan 				// 5
  ColorBlue 				// 6
  ColorPurple 				// 7
)

  

As can be seen, after addition of ColorGray, and start from ColorCyan enumerated value corresponding to the latter have changed. If the value of each enumerated in the code has been hard code, that such an adjustment would be disastrous.

iota although flexible, but it seems a little too flexible.

See this, you think you already know iota, no, you do not, it also flexible and complicated than you think.

 

iota fancy play

First look at a send sub-themes

const (
	AA = iota
	BB        
	_
	DD
)

  

problem

At this point DD corresponding value is the number?

Under a little reasoning, obviously not 2, because the more a middle underlined.

Yes, DD corresponds to a value 3. Underline where "_" indicates a skip value, corresponding to the original value of this position should be 2, but to obtain it is not important, so use underscore skipped, and this usage is defined in the corresponding Go underlined consistent.

Traversing a collection of such map, there is no need to use key values, can be written as

for _, value := range testMap {
  fmt.Println(value)
}

  

Well, look at the next question

const (
	AA = iota 
	BB        
	_
	DD = iota + 1
	EE
)

  

problem

At this point DD and EE corresponding value is the number?

Different from the previous example, where the re-specified after DD DD + IOTA = 1, i.e., adding 1 to the original data, so in this case DD is 3 + 1 = 4.

EE no later redefined the rules will be postponed DD is incremented by one, namely 5.

If the above a question you got the right answer, then the following is not a problem it is not difficult

const (
  AA = iota 
  BB   _   DD = iota + 1   EE FF = iota + 2 GG = iota )

  

problem

At this time, the corresponding value FF and GG is.

According to the previous question, DD, and EE, respectively corresponding to 4 and 5.

FF first look here, where the attention value FF is not extended EE plus 1, then add 2, if the extended FF = 6 + 2 = 8. But the value is FF 7.

Whenever an enumeration is reset (i.e., iota later use when re-assignment) is required from the first to the current enumerator order, such as from where AA = a number of 0 to FF, FF at this time is the order of 5 , then add 2, i.e., FF = 5 + 2 = 7.

GG values ​​using the above method to give a value of 6.

Note: The above is my anti extrapolated from the results conclusions, the beginning is difficult to understand where each corresponding enumeration value, find the rules, we found that the program ran out of the same values and validation rules.

Let's look at the last question

const (
	AA, BB = iota + 1, iota + 2
	CC, DD
)

  

problem

At this time, AA, BB, CC DD, respectively, and the corresponding value is the number

Here only need to understand a rule, iota per line will be a plus.

So here iota first row is 0, the value corresponding to AA and BB, respectively, and 0 + 0 + 1 = 1 2 = 2.

The following CC, and DD are extended, compared to a corresponding iota incremented, respectively, according to the operational and iota iota + 1 and 2 + 1 + 1 is obtained and 1 = 2 + 2 = 3.

Well, whether it is done above to send sub-themes or send proposition, I want you to be this little thing iota there is a real understanding.

Personal feeling, function to achieve 10 million, then to read first.

 

Back to enumerate

Sometimes we use to enumerate, not only is the definition of its value, but also have a corresponding description information, we know that in Java is relatively easy to achieve, after all, Java already has enumerated the concept.

Go Here we look at two ways to achieve with the description of enumeration.

Use map map

const (
	ColorRed 				= iota
      ColorOrange 		
      ColorYellow 		
      ColorGrassland 	
      ColorCyan 			
      ColorBlue 			
      ColorPurple 		
)

var ColorMap = map[int]string{
	ColorRed:       "赤",
	ColorOrange:    "橙",
	ColorYellow:    "黄",
	ColorGrassland: "绿",
	ColorCyan:      "青",
	ColorBlue:      "蓝",
	ColorPurple:    "紫",
}

  

这样,如果想获取ColorRed对应的描述信息,就可以写成ColorMap[ColorRed]。

定义枚举类型

type Color int
const (
	ColorRed 					Color	= iota
  ColorOrange 		
  ColorYellow 		
  ColorGrassland 	
  ColorCyan 			
  ColorBlue 			
  ColorPurple 		
)

func (c Color) String() string {
switch c {
  case ColorRed:
    return "赤"
  case ColorOrange:
      return "橙"
  case ColorYellow:
      return "黄"
  case ColorGrassland:
      return "绿"
  case ColorCyan:
      return "青"
  case ColorBlue:
      return "蓝"
  case ColorPurple:
      return "紫"
}

  

将颜色枚举定义为Color类型,则所有枚举值都是该类型,如果要获取ColorRed对应的描述信息,就可以写成ColorRed.String()。

这种方式看着更加优雅,也更有Go的味道~

如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!如果您想持续关注我的文章,请扫描二维码,关注JackieZheng的微信公众号,我会将我的文章推送给您,并和您一起分享我日常阅读过的优质文章。

 

Guess you like

Origin www.cnblogs.com/bigdataZJ/p/go-iota.html