Build-web-application-with-golang notes

Source: Internet
Author: User
Tags uppercase letter

2.2 Go Basics

1.: = This symbol directly replaces Var and type, which is called a short declaration. However, it has a limitation that it can only be used inside a function, and it cannot be compiled by using outside of the function, so it is generally used to define global variables in var mode.

2. Rune is the nickname of Int32, and Byte is the nickname of Uint8. Different types of variables are not allowed to assign values or operations to each other, or they will cause compiler errors at compile time.

3. Although int is the length of a bit, int and int32 are not interoperable. There are two types of floating-point numbers float32 and float64 (no float type), and the default is float64.

4. The variable at the beginning of the capital letter is exportable, that is, the other package can read, is a common variable; the lowercase letter begins with a non-exportable, private variable.

5. The function that starts with the uppercase letter is also the same as the public function in class, which is the same as the Common keyword, and the lowercase letter begins with the private function with the private keyword.

6. Because length is also part of the array type, [3]int and [4]int are different types, and arrays cannot change length. The assignment between arrays is the assignment of a value, that is, when an array is passed into a function as a parameter, it is actually a copy of the array, not its pointer.

7. Multidimensional Arrays:

Declares a two-dimensional array that takes two arrays as elements, each of which has 4 elements of type int doublearray: = [2][4]int{[4]int{1, 2, 3, 4}, [4]int{5, 6, 7, 8}}//declarations above can simplify , directly ignoring the internal type easyarray: = [2][4]int{{1, 2, 3, 4}, {5, 6, 7, 8}}
8. Slice is not a real dynamic array, but a reference type. Slice always points to an underlying Array,slice declaration can also be like an array, just without the need for length. Slice is a reference type, so when a reference changes the value of one of its elements, all other references change that value.

From the concept above, slice is like a struct, which contains three elements:

    • A pointer to the starting position of the slice specified in the array
    • length, i.e. the length of the slice
    • Maximum length, which is the length of the slice start position to the last position of the array

9. The Append function alters the contents of the array referenced by slice, thus affecting other slice that refer to the same array. However, when there is no space left in the slice (i.e. (cap-len) = = 0), the new array space is dynamically allocated. The returned slice array pointer will point to this space, and the contents of the original array will remain unchanged, while other slice referencing this array are unaffected.

Unlike other basic types, which are not thread-safe, the mutex lock mechanism must be used when multiple go-routine are accessed.

Make for memory allocations for built-in types (map, slice, and channel). New is used for various types of memory allocations. 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 pointer to the 0 value of the newly assigned type T.

The built-in function make (T, args) has a different function than new (t), making can only create slice, map, and channel, and returns a T type with an initial value (not 0) instead of *t. Essentially, the reason that these three types are different is that references to data structures must be initialized before they are used. For slice, map, and channel, make initializes the internal data structure and populates the appropriate values.

2.3 Processes and functions

1. Go inside switch is equivalent to each case last with a 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.

2. 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) {}






Build-web-application-with-golang notes

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.