An example of iota in the Go language

Source: Internet
Author: User
This is a created article in which the information may have evolved or changed.
package mainimport (    "fmt")type BitFlag intconst (    // iota为0,1左移0位 = 1    Active BitFlag = 1 << iota    // Send <=> Active <=> 1 << iota,此时iota为1,1左移1位 = 2    Send    // Receive <=> Send <=> 1 << iota,此时iota为2,1左移2位 = 4    Receive)func main() {    fmt.Println(Active, Send, Receive)}

Iota is at compile time, the compiler is dynamically replaced by the position of the iota in the code and the Const keyword.

package mainimport (    "fmt")const (    //e=0,f=0,g=0    e, f, g = iota, iota, iota)func main() {    fmt.Println(e, f, g)}

Iota can be understood as the row index of a const statement

package mainimport (    "fmt")func main() {    fmt.Println(iota)}

Compilation error: Undefined:iota.

Iota are pre-declared identifiers, but only in const constant declarations.
How do I feel iota this thing is a go bastard, can only be locked in a place, unlike True/false and other brothers, cannot access it.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.