Hello everyone, I am fried fish.
A very unique thing in the Go language is the iota constant. After the incomplete statistics of a certain fish, many Go developers are transformed from PHP, Java, C++, Python, etc., which is very curious.
Today, Fried Fish will learn in-depth with everyone.
Go syntax
Enumeration constants in Go are created using the iota enumerator. Functionally, the iota keyword represents an integer constant starting from 0; functionally, it can simplify the definition of constants using auto-incrementing numbers, which is very convenient.
Previously define an enumeration value:
const (
a = 0
b = 1
c = 2
)
After Go has the iota keyword:
const (
a = iota
b
c
)
The corresponding value result:
a=0
b=1
c=2
You can even dance:
const (
a = iota
_
b
c
)
The corresponding value result:
a=0
b=2
c=3
You can also play with flowers:
const (
bit0, mask0 = 1 << iota, 1<<iota - 1
bit1, mask1
_, _
bit3, mask3
)
The corresponding value result:
bit0 == 1, mask0 == 0 (iota == 0)
bit1 == 2, mask1 == 1 (iota == 1)
(iota == 2, unused)
bit3 == 8, mask3 == 7 (iota == 3)
design thinking
After having a certain basic understanding of iota, we start to enter our theme and spread our curiosity with fried fish.
- Why is it called iota, what is the abbreviation for?
- Why does Go need iota?
Why is it called iota
In fact, iota is the full name, which has been discussed by many community friends in stackoverflow questions (as expected, there are quite a few curious friends).
Essentially "iota" is the 9th letter of the Greek alphabet. It is typical of mathematical notation, representing a very small thing.
Commonly used in the following scenarios:
- as an iterator in a sum-and-algorithm.
- as a subscript index.
- Used for the imaginary part of complex numbers.
In addition to Go, there are iota constants (designs) in APL, C++, or Scheme, which can be used by everyone.
The signature of Scheme iota is as follows:
iota count [start step]
The effect is to return a list of counted numbers, starting from the starting point and increasing the step size each time. The default start is 0 and the default step is 1.
E.g:
(iota 6) ⇒ (0 1 2 3 4 5)
(iota 4 2.5 -2) ⇒ (2.5 0.5 -1.5 -3.5)
In fact, iota is already a convention name for iterators, which can be considered as a common knowledge in the industry.
Why need to have
There is a clear definition and description of iota in " The Go Programming Language Specification ".
as follows:
In a constant declaration, the predeclared identifier iota represents a continuous untyped integer constant. Its value is the index of each ConstSpec in the constant declaration , starting at 0.
Extracting the core, iota in Go fills in ConstSpec indices, which are the positions of consecutive untyped integer constants.
So Go has its place.
Summarize
In this post, we covered the basic syntax of iota in Go. At the same time, based on historical data, we conducted some research on what iota is, why it is called so, and what is its use.
Also need to think about another problem, not every language has iota. What would happen if there was no iota? Is it reasonable to not exist?
The article is updated continuously, you can read it by searching on WeChat [Brain fried fish]. This article has been included in GitHub github.com/eddycjy/blog . If you are learning Go language, you can see the Go learning map and route . Welcome to Star to urge you to update.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。