Go language variable

Source: Internet
Author: User

The definition of variable one and variable in go language

In the go language, VAR is used to define the variable, var variable name data type, and the variable will have an initial value. , the int type defaults to 0,string, which is an empty string ("") by default.

func variabledefinit () {    varint;  // define only unassigned initial values    var string ;    Fmt. Printf ("%d%q\n", a,s); // 0 ""}

And when initializing a variable, you can omit the type, and the variable gets the type from the initial value.

func variabletypeomit () {    var1;     var 2 ;     var " ABC " ;    Fmt. Println (a,b,s);   // 1 2 ABC}
Second, short declaration variable

A short declaration variable can be used only within a function, and a ": =" Concise assignment statement may override the Var declaration in the case of a definite type. function cannot be used outside the: = method.

func variableshortdef () {    A:1;    B:2;    S:"ABC";    Fmt. Println (a,b,s);}
三、一次 declaring multiple variables

A row can have multiple variables assigned, and each variable can have different data types

func variableinline ()  {    a,b,c,s:1,2,3,"ABC";    Fmt. Println (a,b,c,s);}
Iv. Defining variables Centrally
var (            1;             2 ;             true ;             " ABC " ;    )
Five, local variables and global variables

Local variables: defined within a function, its scope is accessible only within the function body.

Global variables: can be accessed from anywhere within the package.

Order of query variables: Global variables, local variables

" FMT " var " Global Variables "; // declaring a global variable within a package Func Main () {    fmt. Println (globalvariable);}

.

Go language variable

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.