This is a creation in Article, where the information may have evolved or changed.
0. The Go language Visibility principle:
-Function name first letter lowercase to private
-The first letter of the function name is public
1. Passing variable-length parameters
-To add ...
-Can be passed all, or a slice can be passed
-Multi-pass Yes, value pass? Reference Delivery (slice)
2. Defer statements
-The defer statement exits the call to the function registration, and when the function exits, the defer statement executes, regardless of whether the function exits normally.
-Will you save the status? The following call result is: 0
func f () { I:0 fmt. Println (i) i+ + return}
-Defer is handy for cleaning up: releasing resources off and opening resource physical locations together.
Srcfile, err: = OS. Open ("MyFile") defer srcfile.close ()
4. The Go language exception recovery mechanism
-Panic () throws an error (similar to throw/raise)
-Panic () is a built-in function that interrupts the original control flow, executes the delay function in the function, and then enters the exception stream.
-Call Recover () in the defer statement to catch the error.
-How can I capture multiple/multiple exceptions?
5. Use structs in go to implement object-oriented
-You can use new () to create a struct object and allocate storage space for it. Returns a pointer to an object.
-Access members with a dot number.
6. Some properties of struct
-Embedded structure: When defining a field, embed its corresponding type, and this type is also a struct
-The embedded structure directly defines the struct-body variables:
-You can assign an initial value to a variable directly based on the embedding structure
-You can assign a value to a variable directly using new (struct{}) .
-embedded structure defines the value of map, which can be initialized.
MAP1: =Map[string]struct{name string ageint}{ "Teacher":{"Roger", +},"Student":{"Roger", A},}fmt. Println (MAP1)
-Anonymous fields
-Support for types only, without the definition of the write segment name, which is the anonymous field, the anonymous field embeds the struct and the pointer in another struct, but does not provide a field name.
-The embedding of anonymous fields is not inheritance, and there is no concept of inheritance in go.
-Normal/anonymous/Multilevel anonymous fields in the struct are accessed using dot numbers. The lookup is from the outer layers to the inner layer.
-Anonymous fields can take many forms: struct/custom (i.e. type skills [] string)/built-in data type (int), except for the struct itself, all other built-in data types and custom types are OK.
-The name of the anonymous field, accessed from the field when accessing the outer layer, you can add field type prefix to access the Rename field within the anonymous fields.
-Supports anonymous type pointers.
type people struct { name string sex bool}type teacher struct{ // anonymous struct field department String
Initializing anonymous fields
T: = teacher{peopel{"Roger", True}, "Computer Science"}
-
7. Why can I perform an exchange below?
Func (recv* cor) swap () { int = temp,recv.y,recv.x fmt. PRINTLN (*recv)}
8. How will anonymous fields be identified in the struct? Why do you use types to reference them?