Go variables, constants, type aliases

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

The basic types of the Go language are as follows:





Type variable Instance code:

 PackageMainImport "FMT"/ * All variables can use the type inference global variable must use VAR can be Var (), can be parallel, can infer that the local variable is not used Var (), can only be parallel, can infer */funcMain () {//bool boolean--first declared after assignment    varABOOLFmt. Println (a) a =trueFmt. Println (a)//int8/uint8--declaring and assigning values    varBint8=-128    varB1uint8=255Fmt. Println (b) fmt. Println (B1)//byte (uint8 alias) type--system-inferred type mode    varC1 =255    varC2byte=255Fmt. PRINTLN (C1) FMT. Println (C2)//int16/uint16--The simplest way to assign a valueD1: =-32768D2: =65535Fmt. Println (D1) fmt. Println (D2)//int32 (rune)/uint32--var parallel    varE1, E2Int32=-2147483648,2147483647    varE3, E4UInt32=0,4294967295Fmt. PRINTLN (E1, E2, E3, E4)//int64/uint64--var ()    var(F1Int64=-46744073709551616F2, F3UInt64=18446744073709551615,184467443709551615) FMT. Println (F1, F2, F3)//Float type float32/float64--display conversion    varG1float32=1.1    varG2 =int8(G1)varG3 =uint8(G1)varG4Int16=Int16(G1) G5: =uint16(G1)//var g6 bool = int8 (G1)Fmt. Println (G1, G2, G3, G4, G5)//string Type    varH1string="MyString"Fmt. PRINTLN (H1)}

The output is as follows:

falsetrue-128255255255-3276865535-2147483648 2147483647 0 4294967295-46744073709551616 18446744073709551615 1844674437095516151.1 1 1 1 1myString



Type aliases:

 PackageMainImport "FMT"/ * Type alias * /funcMain () {//bool boolean--first declared after assignment    varA []int8Fmt. Println (a)varB[3]int8Fmt. Println (b)varCbyte=127Fmt. Println (c)varDRune=2147483647Fmt. Println (d)varE Text ="MyString"Fmt. Println (E)}//byte uint8 Aliases//rune Int32 aliases//The conversion was actually made in this example typetype(byte int8    Rune UInt32Textstring)

The output is as follows:

[][0 0 0]1272147483647myString



Decimal for numeric to text format, numeric to String type

package mainimport"fmt"import"strconv"func main() {    varint = 65    //将数据转为文本格式,是用65表示的文本A    string(a)    fmt.Println(b)    //数字-->>字符串,返回数字 i 所表示的字符串类型的十进制数。    c := strconv.Itoa(a)    fmt.Println(c)    "myString"    fmt.Println(c)}

The output is as follows:

A65myString



Constant:

 PackageMainImport "FMT"funcMain () {FMT. Println (A, B, C, D, E, F, G, H, I, J, K, M, N) fmt. PRINTLN (AA, BB, CC, DD, EE, FF, GG, HH)}ConstAint=1Constb, c, k =' A ',' A ',"Hhehe"Const(d =1E = d +1f g, H =2,3    //l GH is 2, it can't be output.M, n i ="AAA"j =Len(i))Const(AA ="GGG"BB =IotaCC DD =Iota)Const(EE =IotaFF GG ="AAA"hh

The output is as follows:

1 97 65 1 2 2 2 3 aaa 3 hhehe 2 3ggg 1 2 3 0 1 aaa aaa



Operator:

"fmt"/*a-- a++是作为一个运算符,不是一个表达式而且-- ++只能在后边*/func main() {    a3    a--    fmt.Println(a)    //指向a的p指针    var p *int = &a    fmt.Println(*p)}

The output is as follows:

22
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.