Summary of Golang knowledge points

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

Catalogue [−]

  1. The cost of various types of replication
  2. Types of built-in functions can be used (len, cap, close, delete, make)
  3. Value comparison of built-in container types
  4. Combo Type t{...} Comparison of values
  5. 0 value is the type of nil
  6. Functions to be executed at compile time
  7. Values that cannot be addressed
  8. Types that do not support comparisons
  9. Source code elements that can be named
  10. Named source code elements can use the () Group declaration
  11. Source code elements that can be declared inside and outside a function
  12. An expression that can return an optional bool return value
  13. Method of blocking current goroutine with channel mechanism forever
  14. Several ways to connect strings

Original: Golang summaries by Tapirliu

This article is summarized by Tapirliu some of the Golang in the knowledge points, for in-depth study golang very helpful, so I deliberately translated a bit.

The cost of various types of replication

This section title can also be called "size of various types of values" (the sizes of values of all kinds of types), the size of the underlying data that can be shared by different values is not computed.

In the table below word , one represents 4 bytes in the 32bit operating system, 8 bytes in the 64bit operating system, and the content is based on the official go 1.7 compiler.

Cost of
Typevalue Copying (value Size)
bool 1 byte
int8, Uint8, byte 1 byte
Int16, UInt16 2 bytes
Int32, UInt32, Rune 4 bytes
Int64, UInt64 8 bytes
int, uint, uintptr 1 word
String 2 words
Pointer 1 word
Slice 3 words
Map 1 word
Channel 1 word
function 1 word
Interface 2 words
struct The sum of sizes of all fields
Array (element value size) * (array length)

Types of built-in functions can be used (,,,, len cap close delete make )

Make
Len Cap Close Delete
String Yes
Array (and array pointer) Yes Yes
Slice Yes Yes Yes
Map Yes Yes Yes
Channel Yes Yes Yes Yes

All of the above types can use range traversal.

The type that can be used as len a function parameter is also called a container type.

Value comparison of built-in container types

The value of the container is assumed to be addressable (addressable).

The
Type variable length elements can be updated element Addressable find changes the length of the containerunderlying elements can be shared
String No No No No Yes
Array No Yes Yes No No
Slice No Yes Yes No Yes
Map Yes Yes No No Yes
Channel Yes No No Yes Yes

T{...}value comparisons for combined types

Type
( T ) T{} is a T 0 value of the types?
struct Yes
Array Yes
Slice No (0 value is nil )
Map No (0 value is nil )

0 value is nil the type

Type (T) Size of T (nil)
Pointer 1 word
Slice 3 words
Map 1 word
Channel 1 word
function 1 word
Interface 2 words

The size of these types of 0 values remains the same as the size of the above types.

Functions to be executed at compile time

If the function is executed at compile time, then its return value is constant.

Function return value compile-time calculation?
Unsafe. Sizeof uintptr Yes, always
Unsafe. Alignof
Unsafe. Offsetof
Len int Sometimes it is.

The Go specification mentions:
  • If S is a string constant, it len(s) is a constant.
  • If S is an array or an array pointer, it len(s) is a constant.
Cap
Real float64
(Default type)
Sometimes it is.

Go specification: If s It is a complex constant, then real(s) and imag(s) is a constant.
Imag
Complex complex128
(Default type)
Sometimes it is.

The GO specification says: If sr and si both are constants, they complex(sr, si) are constants.

Values that cannot be addressed

The following values cannot be addressed (addresses):

    • Bytes in strings: Byte in string
    • Elements in the map Elements:map
    • Dynamic values of interface values (exposed by type assertions): Dynamic value of the interface
    • Constant values: Constants
    • Literal values: Literal value
    • Package level functions: the function
    • Methods (used as function values): Method
    • Intermediate values: Median
      • function callings
      • Explicit value Conversions
      • All sorts of operations, except pointer dereference operations, but including:
        • Channel receive operations
        • Sub-string operations
        • Sub-slice operations
        • addition, subtraction, multiplication, and division, etc.

Note that &T{} tmp := T{}; (&tmp) the equivalent of the syntactic sugar, so that the &T{} legal is not meant T{} to be addressable.

The following values can be addressed:

    • Variables
    • Fields of addressable structs
    • Elements of addressable arrays
    • Elements of any slices (whether the slices is addressable or not)
    • Pointer dereference operations

Types that do not support comparisons

The following types do not support direct comparisons:

    • Map
    • Slice
    • function
    • struct types containing incomparable fields
    • Array types with incomparable elements

These types that cannot be compared directly cannot be used as key values for the map.

Note: Although the map, slice, and function types do not support direct comparisons, their values can be nil compared directly to each other. If the dynamic type of the two interfaces cannot be compared, the run-time comparison between the two interfaces is panic, unless one of the dynamic values is untyped nil.

Source code elements that can be named

The following source code element can be named, and the name must be Identifier

Can I use it to _ make a name?
Package No
Import Yes
Type Yes
Variable Yes
constant Yes
function Yes
Label Yes

Named source code elements can use the () Group declaration

The following types can be used to () group life

    • Import
    • Type
    • Variable
    • constant

function and label (label) cannot use () grouping declarations.

Source code elements that can be declared inside and outside a function

The following types can be declared inside a function or outside a function:

    • Type
    • Variable
    • constant

importmust precede the declaration of the other element ( package after the statement).

Functions are declared outside of other functions. (Translator Note: function variables/anonymous functions can be declared within a function)

The label must be declared inside the function.

An expression that can return an optional bool return value

The following expression can return an optional bool value:

the meaning of the optional bool return value ignoring optional values can affect the behavior of the program?
Map element Access Does the map contain No
Channel value receive Whether the value received before the channel shutdown has been issued No
Type assertion Whether the dynamic type of the interface conforms to asserted type Yes

(When an optional value is ignored, panic is thrown if the type does not match) |

Method of blocking current goroutine with channel mechanism forever

The following methods can always block the current goroutine:

1. Receive from a channel which no values would be sent to

123
<-make (chanstruct{}) //or<-make (<-chanstruct {})

2. Send value to a channel which no ones would receive values from

123
 Make (chan structstruct{} {}//ormake (chanstruct  struct{} {}

3. Receive value from a nil channel

1
<-Chanstruct{} (nil)

4. Send value to a nil channel

1
Chan struct {} (nilstruct{} {}

5. Use a bare select block

1
Select {}

Several ways to connect strings

The following methods can be used to connect strings (Translator Note: Do not consider performance):

1, use the + connection string. If there are fewer than 6 strings connected, the official compiler optimizes this, so it is generally easy and efficient to use + .
2, use strings strings in the package. The join connection string.
3, use the fmt in the FMT package. Sprintf , fmt. Sprint and fmt. Sprintln the connection string. These three methods can concatenate any type into a string. FMT. Sprintln Adds a space between the strings and a new newline character at the end of the string. If at least one of the two values is not a string, fmt. The Sprint adds spaces between them.
4, Package bytes 's Buffer type (or the built-in function copy ) can be used to build a byte slice, and a byte slice can be easily converted to a string.

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.