Go Language Learning Notes

Source: Internet
Author: User
Tags constant definition function definition

1. Go supports the built-in map type.
2. Go supports array slicing (Slice).
3, the function has more than one return value,
Func getName () {firstname,middlename,lastname,nickname string}{
Return "may", "M", "Chen", "Babe"
}
Because the return value already has a name, each return value can be assigned to a different location in the following way, from
and provides a great deal of flexibility:
Func getName () (FirstName, MiddleName, LastName, nickname String) {
FirstName = "May"
MiddleName = "M"
LastName = "Chen"
Nickname = "Babe"
Return
}
Not every return value must be assigned a value, and a return value that is not explicitly assigned will leave the default null value. And the function of the tune
It is much easier to use than the C + + language:
FN, MN, ln, nn: = GetName ()
If the developer is interested in only a few of the return values of the function, you can also use an underscore directly as a placeholder
Ignore other return values that you do not care about. The following call indicates that the caller only wants to receive the value of the LastName, which prevents the sound
A completely useless variable:
_, _, LastName, _: = GetName ()

4, Go has three keywords for the standard error handling process, respectively defer, panic, recover
5, go does not support inheritance and overloading, but only support the basic type combination function.
6. The go language introduces the concept of Goroutine, which makes concurrent programming very simple. The go language makes concurrent programming lighter and more secure by using goroutine instead of using the concurrency mechanism of the operating system to share memory instead of using shared memory, allowing the function to be executed with the Goroutine method by using the keyword go! Goroutine is a more lightweight and resource-saving process than threading. The go language dispatches the execution of these functions through the system's threads, so that the execution function of each go keyword can be run as a unit. When a concord is blocked, the scheduler automatically arranges the other threads to execute in another thread, which enables the program to run without waiting for parallelism, and the overhead of scheduling is very small.
7. The go language realizes the CSP (communication sequence process) model as the recommended communication mode between Goroutine. In the CSP model, a concurrent system consists of several sequential processes running in parallel, and each process cannot assign values to variables of other processes.
8. All goroutine created within a process run in the same memory address space, so if different
Goroutine has to access shared memory variables, which should be acquired before accessing the corresponding read-write lock. The sync pack in the Go Language standard library provides a complete read-write lock function.
9. The go language reflects most of the functions of reflection, but there is no built-in type factory like the Java language, so it is impossible to create an object instance from a type string like java.
10. The main () function of the go language cannot take parameters or define the return value, the command line passed in the parameters in the OS. Save in the args variable. If you need to support command-line switches, you can use the flag package to define the command-line parameter specification.
11, the definition of Go function begins with the keyword func, and a regular function definition contains the following parts:
Func function name (argument list) (return value list) {
function body
}
A corresponding example is as follows:
Func Compute (value1 int, value2 float64) (Result float64, err error) {
function body
}
Go supports multiple return values, the above example function compute () returns two values, one called result and the other is err. Not all return values must be assigned a value. A value that is not explicitly assigned when the function is returned will be set to the default value, such as the result will be set to 0.0,err to nil.
12. Variable declaration
The variable declaration of the go language differs significantly from the C and C + + languages. For variable declarations, the Go language introduces the keyword VAR, and the type information is placed after the variable name, as shown in the following example:
Var v1 int
Var v2 String
Var v3 [Ten] int//array
Var v4 [] INT//Array slice
Var v5 struct {
f int
}
Var V6 *int//Hands
Var v7 map[string] int//map, key is a string type, value is int type
Var V8 func (a int) int
A variable declaration statement does not need to use a semicolon as a terminator.

Another use of the VAR keyword is that you can put several variables that need to be declared together, lest you need to write the Var keyword repeatedly, as follows:
Var (
v1 int
V2 string
)

13. Variable Initialization
var v1 int = 10
var v2 = 10
V3: = 10//compiler can automatically derive the type of V3
Go supports multiple assignments
I, j = j, I

14. Constant definition
With the const keyword, you can assign a friendly name to a literal constant:
Const Pi float64 = 3.1415
Const ZERO = 0.0//No type floating-point constant
const {
Size Int64 = 1024
EOF =-1//no type integral constant
}

Const u, v float32 = 0, 3//u = 0.0, v = 3.0 constant Multiple assignment
Const A, B, C = 3, 4, "foo"
The constant definition of go can qualify a constant type, but is not required. If you define a constant without specifying a type, it is, like a literal constant, an untyped constant.

15. Pre-defined constants
Go pre-defines these constants: True, False, and iota.
Iota is special and can be thought of as a constant that can be modified by the compiler, which is reset to 0 when each const keyword appears, and then each time a iota appears before the next const appears, the number it represents will automatically increase by 1. For example:
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 at each const start is reset to 0
b = 1 << iota//b = = 2
c = 1 << iota//c = = 4

If the expression of the two Const assignment statement is the same, then the latter assignment expression can be omitted. Therefore, the first two const statements above can be written as follows:
Const (//Iota reset to 0
C0 = Iota//C0 = = 0
C1//C1 = = 1
C2//C2 = = 2
)


16, int and int32 are considered to be two different types in the go language, and the compiler does not do automatic type conversions.
var value2 int32
Value1: =//value1 will be automatically deduced as int type
value2 = value1//Compilation error

17, two different types of integers cannot be directly compared, such as the number of int8 type and the number of int cannot be directly compared, but all types of integer variables can be directly compared with literal constants, such as:
var i int32
var J int64
I, j = 1, 2
if i = = J {//Compile error
Fmt. Println ("I and J are equal.")
}
if i = = 1 | | j = = 2 {//compile through
Fmt. Println ("I and J are equal.")
}

18. The go language defines two types float32 and float64, where float32 is equivalent to the float type of C, and float64 is equivalent to the double type of C language.
Fvalue2: = 12.0//fvalue2 will be automatically deduced as float64
Floating-point numbers cannot be judged equal by = =, and must be done in the following ways:
Import "Math"
P for user-defined comparison accuracy, e.g. 0.00001
Func isequal (F1, F2, p float64) bool {
return Math. Fdim (F1, F2) < P
}

19, do not communicate through shared memory, but should be through communication to share memory.
20. Channel is the way of communication between the goroutine provided by the Go language level. You can use the channel to pass messages between two or more goroutine. Channel is the process of communication, so the process of passing objects through the channel and call the function of the parameter transfer behavior is more consistent, such as the ability to pass pointers.

Go Language Learning Notes

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.