Go language Learning-complex types (1)

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

Pointer

The pointer usage of GO is similar to the use of pointer in C language,& for fetching, * for value. The only difference: no pointer arithmetic

Structural body

Cases:

Type struct_example struct {

a int

b int

}

The struct field is accessed in the same way as the C language, with a dot number.

When you use a struct pointer to access a field, you also use the DOT number to access it.

Array

Definition method: var a[666] int

The use method is similar to C, but the array name in Go is a value, and the array name in C is a pointer. In addition, the array length in go cannot be changed .

If the length is written in the [...],go compiler infers the length according to the defined elements

Example: A: = [...] int{1,2,3,4,5}

Array slicing slice

Slice is like a dynamic array that can change the length.

Slice is the abstraction and control of the underlying array, which contains three kinds of meta data.

1. Pointers to arrays

Length of 2.slice

3.slice capacity, which is the maximum length

Definition Method Example A: = []int{1,2,3,4,5,6}

Given an array or other slice, a new slice is created by means of a[i:j].

This creates a new slice, points to the variable A, starts with the ordinal I, ends with the ordinal j, and the length is J-i

Cases:

Q: = A[1:3]//That is, q is {2,3} slice

You can use the Make function to construct slice

Cases

A: = Make ([]int,2,5)//define a successful slice with a length of 2 and a capacity of 5.

Slice's 0 value is nil.

You can use the Append function to add elements to the slice to make the slice grow longer. Slice will automatically adjust the capacity, that is, adjust the array length, if the appended content length exceeds the current capacity, slice will automatically allocate a chunk of memory large enough.

You can iterate through the slice using the range format. (You can omit an ordinal or value by assigning a value to _ when using range)

You can use the copy function to copy two slice. If these two slice are not of the same length, they will be copied according to the number of elements in the small slice length.

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.