14. Toad Notes Go language--Detail summary One

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

14. Toad Notes Go language--Detail summary One

Slice and arrays differ in declaration: When declaring an array, the length of the array is indicated in square brackets or used ... Automatic

The length is calculated, and when the slice is declared, there is no character in the square brackets.

There are several useful built-in functions for slice:

L Len Gets the length of the slice

L CAP Gets the maximum capacity of the slice

L Append appends one or more elements to the slice and returns a slice of the same type as slice

L copy function copy copies elements from the src of the source slice to the target DST, and returns the number of copied elements

Make is used for memory allocations of built-in types (map, slice, and channel). New is used for various types of memory allocations.

New and make

The built-in function new is essentially the same as the function of the same name in other languages: new (T) allocates 0 of the memory space of the T type populated by the value, and returns its address, which is a value of type *t. In the terms of go, it returns a reference to a

Pin, pointing to the 0 value of the newly assigned type T. One thing is very important:

New returns a pointer.

The built-in function make (T, args) has different functions from new (t), making can only create slice, map, and channel,

and returns a T type with an initial value (not 0) instead of a *t. In essence, these three different types of

The reason is that references to data structures must be initialized before they are used. For example, a slice, which contains a pointer to a number

Three descriptors for pointers, lengths, and capacities of (internal array); Before these items are initialized, the slice is

Nil For slice, map, and channel, make initializes the internal data structure and populates the appropriate values.

Make returns the initialized (not 0) value.

Process Control

Process Control consists of three main categories: conditional judgment, cyclic control, and unconditional jump.

One of the most powerful control logic in Go is for, which can be used to iterate through the data and act as a while to control logic and to iterate.

The for Mate range can be used to read slice and map data.

Go inside switch default equivalent to each case last with break, after the successful match does not automatically down the other case, but instead of the entire switch, but you can use Fallthrough to enforce the following case code.

Function

function is the core design of go inside, it is declared by the keyword func, its format is as follows:

Func funcName (input1 type1, Input2 type2) (output1 type1, Output2 type2) {

Here is the process logic code

Return multiple values

return value1, value2

}

The GO function supports variable parameters. The function that accepts the argument is a variable number of arguments. To do this, you first need to define the function to accept the arguments:

Func myfunc (arg ... int) {}

Arg ... int tells go that the function accepts an indefinite number of arguments. Note that the types of these parameters are all int.

Defer

There is a good design in the go language, the deferred (defer) statement, where you can add multiple defer statements to a function.

When the function executes to the last, these defer statements are executed in reverse order, and finally the function returns.

Panic and Recover

Go has no exception mechanism like Java, it cannot throw exceptions, but uses the panic and recover mechanisms. It should be used as a last resort, that is to say, there should be no or little panic in your code. This is a powerful tool, please use it wisely.

Main function and init function

There are two reserved functions in go: the init function (which can be applied to all the package) and the main function (only

For package main).

Object oriented

The go language can design basic object-oriented programs, but the object-oriented in Go is so simple that there is no private, public key, which is implemented by capitalization (uppercase starts with a common, lowercase start is private), and the method also applies this principle.

Interface

The most subtle design in the Go Language is interface, which makes object-oriented and content organization very convenient. Interface is a set of method combinations, and we define a set of behaviors for an object through interface.

Reflection

The go language implements reflection, so-called reflection is the state of the dynamic runtime. The package we usually use is a reflect bag.

Concurrent

Some people compare go to the C language of the 21st century, the first is because the go language design is simple, second, the 21st century most important is the parallel programming, and go from the language level to support the parallel.

Goroutine is the core of go parallel design.

Goroutine is a thread manager managed through the runtime of Go.

25 Key Words

Break default Func Interface Select

Case defer go map struct

Chan Else goto package Switch

Const Fallthrough If range type

Continue for import return Var

var and const are declarations of variables and constants inside the Go Language Foundation

package and import have had a brief touch.

func for defining functions and methods

return used to return from a function

defer for similar destructors

Go for parallel

Select used to select different types of communication

interface is used to define interfaces,

struct is used to define abstract data types,

break, case, continue, for, Fallthrough, else, if, switch, goto, default inside

Chan for channel communication

type for declaring custom types

map for declaring map type data

range for reading slice, map, channel data

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.