Go Language Learning Notes (b) [variables, types, keywords]

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. Date: July 19, 2014
1, Go in the syntax has a class C feeling. If you want to write two (or more) statements on one line, they must be separated by semicolons. In general, you do not need a semicolon.
2. Go differs from other languages in that the type of the variable is behind the variable name. For example: No, int a, but a int. When a variable is defined, it is assigned a null value of its type by default. This means that after var a int, the value of a is 0. The var s string means that s is assigned a zero-length string, or "".
3, the Go language variable declaration and assignment       use VAR to declare variables in Go,      For example:      var a int   & nbsp   A = 15     var declares the variable a as an int and then assigns a value by =.       Of course in the go language, we can complete the declaration and assignment process in one step,      For example:      A: = 15 (This form can only be used within functions)   & nbsp   In this case, the type of the variable is pushed by the value, the value 15 is the type of int, and the push-show variable A is of type int.            use VAR to declare a set of variables,      For example: Var (          &NBSP ;          a int                     b bool& nbsp              )       If the variable is of the same type, you can declare the variable in the same way as Var, a, or int, but you can also use this method to make the sound Ming and Assignment: A, B: = 20,16      There is a special variable (_, that is, an underscore) in the go language, and any value assigned to it is discarded, which becomes useful in some places.       It is important to note that the Go compiler has an error in declaring a variable that is not used.    4, go language type      1) Boolean type (BOOL): value can be true and false     2) numeric type: int, this type is based on hardware, On a 32-bit machine, int is 32-bit, on a 64-bit machine, int is 64 bits. So is uint.           When we want to specify length, we can use Int32 (UInt32). The complete list of integer types (symbols and unsigned) is Int8,int16,int32,int64 and Byte,uint8,uint16,uint32,uint64. Byte is the alias of Uint8. The values for floating-point types are float32 and float64 (no float type). 64-bit integers and floating-point numbers are always 64-bit, even on 32-bit architectures. Note that these types are all independent, and mixing these types to assign values to variables can cause compiler errors.             For example:            var a int            var b int32            A = 15            B = A + a//compiler will error, prompt (cannot use G + g (type int) as type int32 in Assignment)           for the assignment of numeric types, octal hexadecimal or scientific notation: 077,0XFF,1E3 or 6.022e23 these are all legal.      3) constants: Constant, constants are created at compile time and can only be numbers, strings, or Boolean values.           For example:          Const (                A = iota                B = Iota//iota is an enumeration value, the first time use value is 0, the next time you useThe value is changed to 1, each use will be +1         )           Const (                A = 0                b BOOL = TRUE//explicitly specify the type of the constant          )       4) string             string Assignment: s: = "Hello world!"             The string in Go is a sequence of characters wrapped in UTF-8 by double quotation marks ("). If you use single quotation marks (') it represents a character (UTF-8 encoding), which is not a string in Go. Once you assign a value to a variable, the string cannot be modified: The string is immutable in Go.             How do you do this with changing strings? This is required by:               s: = "hello,world!"                M: = []rune (s)               &NBS P;m[0] = ' C '                S2: = string (m)//actually just newly created a string       &N Bsp        FMT. Printf ("%s\n", S2)           &NBSp; Multiline string:               s: = "Starting part"           &NBS P           + "ending part"                 to be converted to:  &nbs P            s: = "Starting part";                &NB Sp     + "ending part";                This is the wrong syntax and should be written:      & nbsp        s: = "Starting part" +                    &NB Sp "Ending part"               Go will not insert a semicolon in the wrong place.               can also be implemented using anti-quotes, S: = ' starting part            & nbsp                          ,         &NB Sp           &NBsp  ending part '               as the original string character, the value within the quotation marks is not escaped.       5) rune             Rune is an alias for Int32, encoded with UTF-8. When is this type used? For example, you need to traverse characters in a string. You can loop through each byte (only if you use US ASCII-encoded strings with characters equivalent, and they don't exist in Go!). )。 So in order to get the actual characters, you need to use the Rune type.       6) plural             Go native support plural. Its variable type is complex128 (64-bit imaginary part). If smaller is needed, there is also the imaginary part of the complex64–32 bit. The plural is written as re + Imi,re is the real part, IM is imaginary part, and I is Mark  .             For example:            var ss complex64 = 5 + 5i  &NB Sp         FMT. PRINTF ("value is:%v", ss)             Print results: Value is: (5+5i)       7) Error   & nbsp        var e error defines a variable E with an error type whose value is nil. This error type is an interface.
5. The Go language operator go supports the normal numeric operator, which lists the currently supported operators and their precedence.                All of them are combined from left to right. The go language does not support operator overloading (or method overloading), while some built-in operators support overloading.      For example, A + can be used for integers, floating-point numbers, complex numbers, and strings (strings are added to denote concatenation). 6. Go keyword lists all the keywords in go, some have been contacted, others will be introduced later.

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.