Description of BUILTIN package in Golang

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

The BUILTIN package is a pre-declared definition of go, including the various types and method declarations commonly used in the go language, including variables and constants. Its detailed declaration in the Builtin.go file, Link: http://golang.org/src/builtin/builtin.go

First introduce the constants in Golang.

Constant:

Const (        true  //untyped bool.        false //untyped bool.)
True and false are two untyped bool values


Iota //untyped int. untyped int
Iota is a pre-declaration identifier that can only be used in a constant declaration, and its value starts at 0, each new row in the const will cause Iota to count once, i.e. iota itself to grow by 1 (from this point, iota can be seen as the row index in the const block, record the number of rows), its value has been increased by 1 until the next const keyword is encountered, and its value is reset to 0.

1 in a constant declaration, if a constant is not assigned, then he has the same assignment as the previous line, which can be used if more than one constant has its value, only 1 constants are assigned, and the other constants are followed.

Package Mainimport ("FMT") const (A = Iota//iota default initial value is 0b = 100c//c defaults to the same assignment d = Iota//iota default per row plus 1, so when its value is 3) func m Ain () {fmt. Println (A, B, C, D)} Run Result: 0 100 100 3

2 in the variable declaration, each new row, the Iota value is increased by 1, if more than one variable is declared in a row, the Iota value increases only 1 times

Package Mainimport ("FMT") const (A    = Iotab, c = Iota, Iotad    = Iota) func main () {FMT. Println (A, B, C, D)} Run Result: 0 1 1 2

3iota encounters the next const keyword, whose value is re-assigned to a value of 0

Package Mainimport ("FMT") const (A    = Iotab, c = Iota, Iotad    = iota) Const E = iota//encounters the Const keyword again, the Iota value becomes 0func main () {FMT. Println (A, B, C, D, E)} run Result: 0 1 1 2 0

4 constant Definition mode: const name [data type]= expression, where data type is optional, but it must be noted when using data types: Two with type constants cannot be written on the same line, with type constants must be assigned, cannot be empty, as follows:

Const (A = 1 d int//error b int, c int = 1, 1//error)
The correct wording is as follows:

Const (a    int = 1d    intb, c = 1, 1)
So when you define a const constant, you can have no data type unless you specifically emphasize the constant type.

Variable:

Func Append (Slice []type, Elems ... Type) []type

There are two ways of using it:

Slice = append (slice, elem1, elem2)//Add individual elements directly behind slice, add element type can be the same as slice, can also be different slice = append (slice, anotherslice ...) Add another slice directly to the slice, but it is essentially adding an element from Anotherslice to the slice, similar to the first.
As a special case, it is legal to add a string to the byte array.

Package Mainimport ("FMT") func main () {slice: = append ([]byte ("" Hello ")," World "...) In fact, it's normal to think about it, because in go []byte and string can be converted directly to each other. Fmt. Println (String (slice))} Run Result: Hello World


Func len (v Type) int

Len () is a function in go that uses a higher frequency to return the length of type V, its corresponding type, and the value returned as follows:

Array : Array length array pointer: array length slice/map:slice or number of elements in map string: Number of bytes in a string channel: existing quantity in channels

Func Cap (v Type) int

The CAP () returns the capacity of the container, and sometimes the value returned by Len () is different, and its corresponding type and return are as follows:

Array : Array length array pointer: Array length slice:slice can reach maximum length after reallocation Channel: allocates the cache size in channel
By contrast we can see that the values of Len and cap are the same when confronted with array types and array pointers, and are array lengths.

Similarities and differences: The CAP does not support the map,string type. In the slice and channel, the values obtained are also different, Len obtains the existing values, and the CAP obtains the maximum value. Examples are as follows:

Package Mainimport ("FMT") func main () {A: = make (chan int, ten) fmt. Println (Cap (a))        //10fmt. Println (Len (a))        //0b: = Make ([]int, 2) b = Append (b, 1) fmt. Println (len (b))        //3fmt. Println (Cap (b))      


Func Close (c chan<-Type)

Close () can only be used to close the channel, and it can only be closed at the sending end and cannot be closed at the receiving end. See http://blog.csdn.net/chenbaoke/article/details/41647865 for details.


Func Complex (R, I floattype) ComplexType//Converts two floating-point number types to a complex number, where both the real and imaginary parts must be of the same type and can only be used for float32 and float64

Func imag (c ComplexType) Floattype//Get imaginary part of complex number

Func Real (c ComplexType) Floattype//Get the real part of the plural

Package Mainimport ("FMT") func main () {A: = Complex (1, 2) B: = Imag (a) c: = Real (a) fmt. Println (A, B, c)} operation result: (1+2i) 2 1

Func copy (DST, src []type) int

Copy implements replication between two slice, where the length of the copy is relatively small in length in DST and SRC, and returns a relatively small length that allows overwriting during copy.

Package Mainimport ("FMT") func main () {A: = makes ([]int, 1) b: = []int{1, 2}c: = Copy (A, b)//Because the size of a is 1, so b copies 1 element 1 for a and returns C The length is also 1fmt. Println (A, B, c)} operation result: [1] [1 2] 1



Func Delete (M map[type]type1, key Type)

Removes the corresponding element from the map and does nothing if there is no key


Func make (type, size Integertype) Type

Only for Map,slice,channel

Func new (Type) *type//Initializes the object and returns a pointer to the object


Func Panic (v interface{})//Stop Goroutine execution, first execute the defer function, wait for the defer function to finish executing, send the error message to its panic caller panic related information.
The Func recover () interface{}//defer function obtains the panic call error message through recover and resumes normal execution. If there is no panic, then recover will return nil.


Func print (args ... Type)
Func println (args ... Type) //Do not understand its usage for the time being

Type ComplexType//denotes all complex types complex64 or complex128
Type Floattype//represents all floating-point types: float32 or float64
Type Integertype
Type type
Type Type1
type bool
Type byte//equivalent with UINIT8, which is used to differentiate between byte values and 8-bit unsigned integers
Type complex128
Type complex64
Type error
Type float32
Type float64
type int//signed integer, whose size is related to the number of machine buses, but he is a specific type, not an alias of Int32 or Int64.
Type Int16
Type int32
Type Int64
Type int8
Type Rune//int32 alias, which is used to differentiate between character values and integer values
The type string//8-bit byte is a collection of strings that can be empty, but cannot be nil and cannot be modified on string.
Type UINT
Type UInt16
Type UInt32
Type UInt64
Type Uint8
Type uintptr//is a bit pattern that is large enough to hold any pointer.


var nil type      //nil can only represent pointers, channel, function, Interface,map, or slice types and cannot represent other types.



Reference: http://golang.org/pkg/builtin/

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.