Golang basics and golang Basics
1. Inheritance overload is not supported. For example, for C ++ Java interfaces, modification of interfaces will affect the modification of class behavior of the entire interface to be modified. Go designers think this feature may be useless. 2. Any function definition must be followed by curly brackets after the function declaration and cannot be wrapped in a line break such as func funca (a int ){}, in the Go language, functions are also a type that can be deduced and used to support anonymous functions and closures. Function return values support multiple responses similar to Python. If no value is assigned to the integer type, the default value is 0. The default value is 0.0 error. The default value is nil3. Do not introduce unnecessary packages. This is the Go principle, just like the strict Tab character of Python, both the unused variable and the unused variable are reported by the compiler. 4. package is used as an introduction package. 5. import "a" calls the function a in the package. xx () 6. CGo is a feature of the Go language. It is easier to call C in the Go language to implement C in comparison to Java JNI. 7. go uses goroutine for coroutine optimization to improve concurrency performance. Dynamic thread adjustment. 8. 6 GB and 6l are 64-bit Go compilers and connectors. The 32-bit tools are 8 GB and 8 L. Go also has another GCC version compiler named gccgo. 9. Note that multiple go files can use the same package name. to generate an executable file for Go, you must declare the package mainmain function func main () {args: = OS. args if args = nil | len (args) <2 {return} 10. To be able to build this project in Linux, add the root directory of the project to the environment variable GOPATH. Assume that the calcproj directory is located in ~ /Goyard, edit ~ /. Bashrc file, and add the following line of code: export GOPATH = ~ /Goyard/calcproj and then execute the following command to apply this setting: $ source ~ /. Bashrc, GOPATH and PATH environment variables are the same, you can also accept multiple paths, and the paths and paths are separated by colons. After setting GOPATH, we will start to build the project. Suppose we want to put the generated executable file in the calcproj/bin directory. 11. GoRoot go installation path .... in the above example, the project path 12 and GDB debugging do not need to set any compilation options. The binary program compiled in the Go language directly supports GDB debugging, for example, the executable file calc compiled by go build calc, run the following command directly in debug mode: $ gdb calc because the standard usage of GDB is not particularly related to Go, It is not detailed here, interested readers can view the corresponding documents on their own. Note that the debugging information generated by the Go compiler is in the format of DWARFv3, which should be supported by GDB Versions later than 7.1. 13. The Go language is called "Better C Language" 14. Each line does not need to add a plus sign. 15 is automatically added. map [string] int 16 is added. Type derivation var a int is added. = 1 is equivalent to: = 1 var v1 int = 10 // correct usage 1 var v2 = 10 // correct usage 2, the compiler can automatically export the v2 type v3: = 10 // correct usage 3. the compiler can automatically export the v3 type
The variable that appears at: = On the left should not have been declared; otherwise, compilation errors may occur. For example, the following method is used: var I inti: = 2 will lead to compilation errors similar to the following: no new variables on left side of: = 17. Support for multiple assignments, I, j = j, I two values can be exchanged in such a simple way without introducing external variables.
18. This is often the case when we use traditional strong language programming, that is, to obtain a value when calling a function, however, a bunch of useless variables have to be defined because the function returns multiple values. In Go, this type of code can be avoided by combining multiple return and anonymous variables to make the Code look more elegant. Assume that the GetName () function is defined as follows. It returns three values: firstName, lastName, And nickName: func GetName () (firstName, lastName, nickName string) {return "May", "Chan", "Chibi Maruko"} If you only want to obtain the nickName, you can write the function call statement as follows: _, _, nickName: = GetName () this usage can make the code very clear, basically blocking the content that may confuse the code reader's sight, thus greatly reducing the complexity of communication and the difficulty of code Maintenance
19. In the Go language, constants are known and unchangeable values during compilation. Constants can be numerical (including integer, floating point, and plural), Boolean, and string types. Literal is a hard-coded constant in a program, for example: -123.14159265358979323846 // constant of the Floating Point Type 3.2 + 12i // constant of the plural type true // constant of the Boolean Type "foo" // String constant in other languages, constants usually have specific types. For example, limit 12 is considered to be an int type constant in C. If you want to specify a long type constant with the value of limit 12, you need to write it as-12l, which is a bit contrary to people's intuitive feeling. The literal constant of Go language is closer to the constant concept in our natural language. It is non-typed. As long as the constant is within the value range of the corresponding type, it can be used as a constant of this type, such as the above constant-12, it can be assigned to int, uint, int32, int64, float32, float64, complex64, complex128, and other types of variables.
20. Through the const keyword, you can specify a friendly name for a literal constant: const Pi float64 = 3.14159265358979323846 const zero = 0.0 // non-Type Floating Point constant const (size int64 = 1024 eof =-1 // non-type integer constant) const u, v float32 = 0, 3 // u = 0.0, v = 3.0, multiple constant values const a, B, c = 3, 4, "foo" // a = 3, B = 4, c = "foo", the constant definition of Go without a type Integer or a String constant can limit the constant type, but is not required. If the type is not specified when a constant is defined, it is a non-type constant like a literal constant. The right value of a constant definition can also be a constant expression that is computed during the compilation period. For example, const mask = 1 <3 because the assignment of a constant is a compilation period, therefore, the right value cannot contain any expressions that require running to produce results. For example, if you try to define a constant in the following way, the const Home = OS will be incorrect. the reason for GetEnv ("HOME") is very simple, OS. getEnv () can only know the returned results at runtime. It cannot be determined during compilation, so it cannot be the right value defined as a constant.
21. The Go language predefines these constants: true, false, and iota.
Iota is special and can be considered as a constant that can be modified by the compiler.
Reset to 0, and the number represented by iota is automatically increased by 1 every time before the next const appears.
The following example shows how to use iota:
Const (// iota is reset to 0
C0 = iota // c0 = 0
C1 = iota // c1 = 1
C2 = iota // c2 = 2
)
Const (
A = 1 <iota // a = 1 (iota is reset to 0 at the beginning of each const)
B = 1 <iota/B = 2
C = 1 <iota/c = 4
)
Const (
U = iota * 42 // u = 0 v float64 = iota * 42 // v = 42.0
W = iota * 42 // w = 84) const x = iota // x = 0 (because iota is reset to 0 again) const y = iota // y = 0 (same as above) if the two const assignment statements have the same expression, you can omit the next assignment expression. Therefore, the first two const statements can be abbreviated: const (// iota is reset to 0 c0 = iota // c0 = 0 c1 // c1 = 1 c2 // c2 = 2) const (a = 1 <iota // a = 1 (iota is reset to 0 at the beginning of each const) B // B = 2 c // c = 4) 22. For enumeration, all symbols starting with uppercase and lowercase characters that are visible outside the package can only be used inside the package. Enumeration refers to a series of related constants. For example, the following is the definition of each day in a week. Through the example in the previous section, we can see that a group of constants can be defined in the const followed by a pair of parentheses. This definition is usually used in the Go language to define enumeration values. The Go language does not support the enum keywords explicitly supported by many other languages. The following is a general enumeration notation, which defines a series of Integer constants: const (Sunday = iota Monday Tuesday Wednesday Thursday Friday Saturday numberOfDays // This constant is not exported) like other symbols in the Go language, constants starting with an uppercase letter are visible outside the package. In the preceding example, numberOfDays is private in the package, and other symbols can be accessed by other packages.
23. The boolean type in go can only be false true! ===Cannot be forcibly converted. The boolean type in the Go language is basically the same as that in other languages, and the keyword is also bool. The sample code that can be assigned to the predefined true and false values is as follows: var v1 boolv1 = truev2: = (1 = 2) // v2 will also be deduced as bool type boolean type. Values of other types cannot be assigned, automatic or forced type conversion is not supported. The following example shows some incorrect usage and may cause compilation errors: var B boolb = 1 // compilation error B = bool (1) // compilation error: var B boolb = (1! = 0) // compile fmt correctly. Println ("Result:", B) // print the Result as Result: true
24. The Go language supports the following comparison operators: >,<,=, >=, <=, and! =. This is the same as most other languages and is exactly the same as the C language. If I! = J {} // must contain braces I, j: = 1, 2 if I = j {fmt. println ("I = j");} else {fmt. println ("I! = J ");} 25. Two values of different types cannot be compared, for example, int8 int16. They can only be forcibly converted and then compared to var a int8 var B int16, B = 1, 2 if int16 (a) = B {fmt. printf ("a = B")} 26. Although two int8 int16 values cannot be directly compared, any integer type can be compared with the word surface constant integer, however, it cannot be compared with the string literal constant var a int16 if a = 1 {fmt. printf ("! = \ "\"");}
27. Pay attention to ~ . Go has changed to ^ Go which supports the bitwise operators shown in Table 2-2. Table 2-2 operation examples x <y shifted to 124 <2 // The result is 496 x> y shifted to 124> 2 // The result is 31 x ^ y. or 124 ^ 2 // The result is 126 x & y and 124 & 2 // The result is 0 x | y or 124 | 2 // The result is 126 ^ x/ /The result is-3. Most bitwise operators in the Go language are similar to those in the C language, except in C ~ X, while ^ x in the Go Language
28. The floating-point operation on floating-point numbers is automatically derived from float64. That is, the double type in C language cannot be directly converted to fload32. the floating-point type is used to indicate the data containing the decimal point, for example, 1.234 is a floating point data. Floating point type in Go Language
Standard expression of IEEE-754 was used.
1. floating point representation
The Go language defines two types of float32 and float64. float32 is equivalent to the float type of C language,
Float64 is equivalent to the double type in C language.
In the Go language, the code for defining a floating point variable is as follows:
Var fvalue1 float32
Fvalue1 = 12
Fvalue2: = 12.0 // if no decimal point is added, fvalue2 is deduced as an integer rather than a floating point type.
In the above example, the type of fvalue2 is automatically deduced, note that its type will be automatically set to float64,
Regardless of whether the number is represented by a 32-bit length. Therefore, for the above example, the following assignment will cause Compilation
Error:
Fvalue1 = fvalue2
For fvalue2, which is automatically deduced in the above example, it should be noted that its type will be automatically set to float64, regardless of whether the number assigned to it is expressed in 32-bit length. Therefore, for the above example, the following value assignment will cause a compilation error: fvalue1 = fvalue2 and must use this forced type conversion: fvalue1 = float32 (fvalue2) 29. Comparison of custom precise floating point numbers. Because floating point numbers are not an accurate expression, the comparison accuracy may be inaccurate because floating point numbers are not an accurate expression, therefore, using = as an integer directly determines whether two floating point numbers are equal is not feasible, which may lead to unstable results. The following is a recommended alternative: import "math" // p for user-defined comparison precision, such as 0.00001 func IsEqual (f1, f2, p float64) bool {return math. fdim (f1, f2) <p}
30. In the Go language, the real-time imag is used to retrieve the plural number of the virtual part with 0 as the pure virtual part. A type of number can be expressed as this type of plural var v1 complex64 v1 = 2.5 + 15i v2: = 2.5 + 15i v3: = complex (2.5, 15) fmt. println (v1) fmt. println (v2) fmt. println (v3) fmt. println ("real:", real (v1) fmt. println ("real:", imag (v1 ))
The plural is actually composed of two real numbers (represented by floating-point numbers in the computer). One represents real and the other represents virtual parts (imag ).
For what is a plural, refer to: http://baike.baidu.com/view/10078.htm
The plural is actually composed of two real numbers (represented by floating-point numbers in the computer). One represents real and the other represents real.
Virtual part (imag ). If you understand what the plural is in mathematics, then the plural of the Go language is very easy to understand.
1. plural representation
An example of the plural expression is as follows:
Var value1 complex64
// The Plural type consisting of two float32
Value1 = 3.2 + 12i
Value2: = 3.2 + 12i
// Value2 is of the complex128 type
Value3: = complex (3.2, 12)
// Value3 returns the same result as value2
2. real and virtual
For a complex z = complex (x, y), you can use the Go language built-in function real (z) to obtain the real number of the complex.
, That is, x, obtain the virtual part of the complex number through imag (z), that is, y.