Strange Go Language Iota

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Go language iota A bit strange, look at the following code:

Package MainImport ( "FMT")Const ( testmin =1TestAtestb = Iota testc)                func Main(){FMT. Printf("testmin:%d\n", testmin)        FMT. Printf("testa:%d\n", TestA)        FMT. Printf("testb:%d\n", testb)        FMT. Printf("testc:%d\n", testc)}

What is the execution result of the above code?

As a result, when you see the results, the usage of iota is basically mastered:

/*TestMin:-1TestA:-1TestB:2TestC:3*/

Let's look at one more example:

Package MainImport ( "FMT")Const (i=1<<iota j=3<<iota k l)func Main(){FMT. Println("i=", i)  FMT. Println("j=", J)  FMT. Println("k=", K)  FMT. Println("l=", L)}

Execution Result:

i= 1j= 6k= 12l= 24

K equals 12,l equals 24 How is it got out?

    i=1<<iota    j=3<<iota    k    l

That is

    i=1<<0    j=3<<1    k=3<<2    l=3<<3

Iota each occurrence, automatically add 1, and the preceding operand if not specified, the default is to use the previous one, here is 3;

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.