Go Learning notes: 1.1 variables

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


/**data:2015-06-02*author:qdx*note:go language Learning Note: variable */package mainimport "FMT" Variable type//go language:/*boolstringint int8 int16 Int32 int64uint uint8 uint16 uint32 uint64uintptrbyte (equivalent to uint8) Rune (equivalent to Int32, used to represent a Unicode code point) float32 float64c       Omplex64 Complex128*///1.go language, the definition of a variable is the beginning of Var, followed by the variable name and type, can not be defined repeatedly//a;   Error, variable not defined//var A; Error, no add variable type, and no initial value (the initial value can be regarded as stealth declaration) var a int = 5;//2. Any structure outside the function (construct) begins with a keyword, such as variable//amount starting with the Var keyword, and the function starts with the FUNC keyword//a   = 6;  Error, the function outside the body does not define the statement//a: = 6;  Error: The left of = does not define a new variable, the function is outside the body of the non-defined statement//b: = 6; Error, the function outside the body of the non-definition statement, must start with the keyword//var b: = 6;  Error, syntax errors, define variable initialization with "=" instead of ": =" var b = 6;//g: = 9; Error, you must start with the keyword//3. When defining a variable, you can use only one Var, and the definition of all variables in parentheses var (c int = 8;d int = 9;e = 10;f string = "Hello";)//var I, j, k = 10, 20 ;  When the definition is initialized, the number of left and right parameters must be the same as Var I, j, k = ten, 30;//var l int, m string;       //4 cannot be defined in the same row when the type is inconsistent. The variable definition in the body of a function can not start with Var, func test () {//b: = 8; All variables in the body of the function must be used, otherwise the error is, the function body can be defined as Var a, b int = 10, 20;   Variables in the body of the function can be overridden within the scope of the function to override the variable//var a = 50 in the function body;        Error, the function body has a repeating definition g: = 10; //The function body can define the variable without VAR, at which time it has been//g: = "Hello"//variable definition, the type is determined, if you need to assign different types of values to the defined variables, you need to cast//g = int (6.5);     Error, cannot convert character constant to type var h = 6.5;g = Int (h); To cast the variable correctly, the type conversion of the variable must be the displayed T (Var) fmt. Println (a); fmt. Println (b); fmt. Println (f); fmt. Println (e+10);//fmt. Println (e+ "a"); Error FMT. Println (g);} Func Testconst () {///Test constants//function body constants can not be used, the constants defined within the function are used only within the function,//Beyond the definition of the constant function scope, the scope of the constant disappears const PI = 3.14const (A = 5;c = "Golang" ;)}func Main () {test (); Testconst (); fmt. Println ("C:", c); return;}





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.