Go language learning notes (2) [variables, types, and keywords]

Source: Internet
Author: User

Date: January 1, July 19, 2014

1. the syntax of Go is similar to C. If you want to write two (or more) Statements in one row, they must be separated by semicolons. Generally, you do not need a semicolon.

2. The difference between go and other languages lies in that the variable type 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. VaR s string means that S is assigned a zero-length string, that is "". 3. Declaration and assignment of variables in go declare variables using VAR. For example, var a int A = 15 var declares variable A as int type, then assign a value through =. Of course, in the go language, we can complete the Declaration and assignment process step by step, for example, a: = 15 (this form can only be used within the function). In this case, the variable type is derived from the value. The value 15 is of the int type, and the streamer variable A is of the int type. You can use VaR to declare a set of variables. For example, if VAR (A int B bool) has the same type, you can use VaR a, B INT to declare the variables, of course, you can also declare and assign values in this way: A, B: = 20, 16 there is a special variable in the go language (_, that is, underline ), any value assigned to it is discarded, which will become very useful in some places. Note that the go compiler reports an error for variables that are declared but not used. 4. Type of Go language 1) boolean type (bool): The value can be true or false. 2) numeric type: Int. This type depends on the hardware. On a 32-bit machine, int Is 32-bit. On a 64-bit machine, Int Is 64-bit. The same applies to uint. Int32 (uint32) can be used to specify the length ). The complete list of integer types (signed and unsigned) are int8, int16, int32, int64 and byte, uint8, uint16, uint32, and uint64. Byte is the alias of uint8. Floating point values include float32 and float64 (no float type ). 64-bit integers and floating-point numbers are always 64-bit, even in a 32-bit architecture. Note that all these types are independent, and assigning values to variables using these types together will cause compilation errors. For example, var a int var B int32 A = 15 B = a + A // The Compiler reports an error, prompting (cannot use g + g (type INT) as type int32 in assignment) values of the numeric type can be octal, hexadecimal, or scientific notation 077, 0xff, 1e3, or 6.022e23. 3) constant: constant, which is created during compilation. It can only be a number, string, or Boolean value. For example, const (A = iota B = iota // iota is an enumerated value. The value of the first time it is used is 0, and the value of the next time it is used is 1, and the value of each time it is used is + 1) const (a = 0 B bool = true // specify the type of the constant) 4) String Value assignment: S: = "Hello world! "String in go is the character sequence of the UTF-8 enclosed by double quotation marks. If you use single quotes ('), it represents a character (UTF-8 encoding), which is not a string in go. Once a variable is assigned, the string cannot be modified: In go, the string is unchangeable. So how can we change the string? Use the following method: S: = "Hello, world! "M: = [] Rune (s) m [0] = 'C' S2: = string (m) // actually only creates a new string FMT. printf ("% s \ n", S2) multi-line string: S: = "Starting part" + "ending part" will be converted to: S: = "Starting part "; + "ending part"; this is the incorrect syntax. Write "s: =" Starting part "+" ending part "go" to avoid inserting a semicolon in the wrong part. You can also use reverse quotation marks. S: = 'starting part ending part' is the original string character, and the characters whose values are enclosed in quotation marks are not escaped. 5) Rune rune is the alias of int32, Which is encoded by UTF-8. When will this type be used? For example, you need to traverse characters in a string. Each byte can be cyclically (only when the us ascii encoding string is used and the character is equal to the price, but they do not exist in go !). Therefore, to obtain the actual characters, the rune type must be used. 6) The Plural go native supports the plural. Its variable type is complex128 (64-bit imaginary part ). If you need a smaller value, there is also the complex64-32-bit imaginary part. The complex number is written as RE + IMI, the Re is the real part, the IM is the virtual part, and the I is the mark. For example, var ss complex64 = 5 + 5I FMT. printf ("value is: % v", SS) print result: value is: (5 + 5I) 7) Error var e error defines an error type variable e, the value is nil. This error type is an interface. 5. The go operator supports common numeric operators. It lists the currently supported operators and their priority. They are all from left to right. The Go language does not support Operator Overloading (or method overloading), but some built-in operators support overloading. For example, + can be used for integers, floating-point numbers, plural numbers, and strings (strings are added together to concatenate them ). 6. All the keywords in go are listed in the go keywords, some of which have been touched and others will be described 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.