4. Toad notes Go language-variables

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

4. Toad notes Go language-variables

The VAR statement defines a list of variables, just like the argument list of a function, the type is behind.

As seen in this example, the ' var ' statement can be defined at the package or function level.

The code is as follows:

Package Main

Import "FMT"

var c, Python, Java bool

Func Main () {

var iint

Fmt. Println (i,c, Python, Java)

}

The results of the implementation are as follows:

0 false false

Initialize variables

A variable definition can contain an initial value, and each variable corresponds to one.

If the initialization is using an expression, you can omit the type, and the variable gets the type from the initial value.

Package Main

Import "FMT"

var i, J int = 1, 2

Func Main () {

var C,python, Java = True, False, "no!"

Fmt. Println (I,j, C, Python, Java)

}

The results of the implementation are as follows:

1 2 True False no!

Short Declaration variable

In a function, the ': = ' concise assignment statement can be used instead of the VAR definition where it is explicitly typed.

Each statement outside the function must start with a keyword (' var ', ' func ', and so on), ': = ' structure cannot be used outside the function.

For example:

Package Main

Import "FMT"

Func Main () {

var i,j int = 1, 2

K: = 3

C,python, Java: = True, False, "no!"

Fmt. Println (I,j, K, C, Python, Java)

}

The results of the implementation are as follows:

3 true False no!

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.