Common functions of the Go language

Source: Internet
Author: User

1 Make function to create an array slice

The built-in function make () provided by the go language can be used to flexibly create array slices.
Create an array slice with an initial element number of 5, with an element initial value of 0:
MySlice1: = Make ([]int, 5)
Create an array slice with an initial element number of 5, an element with an initial value of 0, and a storage space of 10 elements:
MySlice2: = Make ([]int, 5, 10)

2 Defer statements

The defer statement is called at the end of the function, even if an exception occurs after the statement runs, the defer statement is still executed.

It is important to note that if a parameter is referenced in the defer statement, the value of the parameter will be the value of the program to the defer line, which is not related to the subsequent statement. 3

The range function is a magical and interesting built-in function that you can use to iterate through arrays, slices, and dictionaries.

When used to iterate over arrays and slices, the range function returns indexes and elements;

When used to traverse a dictionary, the range function returns the key and value of the dictionary.

4 func (PS *peerset) peerswithoutblock (hash common. Hash) []*peer

struct function, preceded by a function name, is the recipient of the method

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.