Golang Pit Set

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Func Main () {x: = 1defer func (a int) {FMT. Println ("A=", a)} (x) defer func () {fmt. Println ("x=", X)} () x + +}

Results: x=2 a=1

Note: The required parameters of the defer call are computed when the defer statement executes (copy pass), inside the closure is a reference, defer execution order FIFO

Arrays and Slice

An array is a value type slice is a reference type,

Array creation arr:=[...] int{1,2,3} arr:=[3]int{} arr:=new ([10]int) (Note that new returns a pointer class of [10]int])

Slice creation slice:=[]int{1,2,3} Slice:=arr[n:m] (n <= x < m) Slice:=make ([]int,len,[cap]) (Cap Default=len)

Each slice points to an underlying array, and updates to the slice are reflected on the underlying array, cap=slice the start_index of the array to the array End_index

Reslice Slice1:=slice2[n:m] If n,m exceeds the cap when slice is reslice, it will not cause redistribution of the underlying array, but will panic:runtime error:index out of range

Append (s1,1,2,3) can raise the underlying array redistribution by adding a cap

Copy (DST,SRC) len=min (Len (DST), Len (SRC))

Map

D: = map[*int]struct{x, y float64} {&10: {1.0, 2.0}}


struct and array are also value types

Type s1 struct {s2 struct {a int}}func main () {m: = map[int]struct{a int}{1: {1}}//correct FMT. Println (m) S: = s1{{1}}//exception missing type in composite literalfmt. Println (s)}
The anonymous struct can be used as the value of the map and is easy to assign, while the anonymous struct as a member of the struct requires the name of the struct when it is initialized, and the member variable anonymous struct is inconvenient.

struct Copy copy

U: = user{, "Tom"}var u2 *user = new (User) *U2 = u


Anonymous members (combination better than inheritance)

Type D1 struct {x int}type Data struct {d1x int}func main () {d: = data{d1{10}, 20}d.x = 200d. d1.x = 100fmt. Println (d)}


Interface type inference

If O,ok:=i. (User); ok{     o.xxx}switch o:=i. (type) {case    User:          xxx Case man    :          xxx}

Value copy behavior occurs when an object is assigned to an interface variable

U: = user{1, "Tom"}i: = Tester (U)//cast assignment occurs with copy action//in Golang to be sure that this assignment is a copy action, and that if the Copy object is a pointer, the performance will be better. ID = 100//Explicit modification of the original object U. Name = "Jack" FMT. Println (U, i)//Although the replicas in the ⼝ mouth are not affected





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.