Go language note--append is a built-in function!!! New is a Function!!! Debugging can use closures, essentially print debugging, and so on!

Source: Internet
Author: User

Built-in functions

The Go language has built-in functions that you can use without the need for an import operation. They can sometimes operate on different types, such as Len, Cap, and append, or must be used for system-level operations, such as panic. Therefore, they need to be supported directly by the compiler.

Here's a simple list, which we'll explain in more detail in a later chapter.

name Description
Close For pipeline communication
Len, Cap Len is used to return the length or number of a type (string, array, slice, map, and pipe); cap is the meaning of capacity to return the maximum capacity of a type (only for slices and maps)
New, make New and make are used to allocate memory: New is used for value types and user-defined types, such as custom structures, and make is used for built-in reference types (slices, maps, and pipelines). They are used like functions, but type is the parameter: new (type), make (type). New (t) assigns a 0 value of type T and returns its address, which is a pointer to type T (see section 10.1). It can also be used for basic types: v := new(int) . Make (t) returns the value after the initialization of type T, so it does more work than new (see section 7.2.3/4, 8th 1.1 and 14th. 2.1) New () is a function, do not forget its parentheses
Copy, append For copying and joining slices
Panic, recover Both are used for error handling mechanisms
Print, println The underlying print function (see section 4.2), recommends the use of the FMT package in the deployment environment
Complex, Real imag Used to create and manipulate complex numbers (see section 4.5.2.2)

6.10 Debugging with closures

When you are parsing and debugging complex programs, countless functions are called each other in different code files, and it is helpful to debug if you know exactly which function is executing in which file. You can use &NBSP, runtime   or   log   Special functions in the package to implement such a function. Package   runtime   function   Caller ()   provides the appropriate information so that a   can be implemented when needed; where ()   closure function to print the location of function execution:

Func () {: = runtime.  Caller (1) log.  Printf ("%s:%d", file, line)}where ()//Some codewhere ()//Some more code  where ()            

You can also set log the flag parameter in the package to implement:

Log. setflags (log.  Llongfile) log. Print ("")    

Or use a shorter version of the where function:

where = log. Printfunc1 () {where () ... some codewhere () ... some codewhere ()}    

Calculate function Execution Time

Sometimes it is interesting to know how long it takes to calculate the execution time, especially in contrast and benchmark tests. The simplest way to do this is to set a starting time before the calculation starts, then the end time at the end of the calculation, and finally the difference between them, which is the time spent in this calculation. To implement this, you can use the time and functions in the package Now() Sub :

: = time. Now ()longcalculation (): = time.  Now (): = end.  Sub (Start) fmt. Printf (%s\ n", Delta)         

Excerpt from: https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/06.10.md

Go language note--append is a built-in function!!! New is a Function!!! Debugging can use closures, essentially print debugging, and so on!

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.