This is a creation in Article, where the information may have evolved or changed.
2. Bubble sort (used by two-dimensional arrays):
Func Main () {i: = 1mylabel:for {i++if i > 3 {break mylabel}}fmt. Println ("MYLABEL")}//label name is arbitrary and must be written in front of the For loop
2. Bubble sort (array use):
Func main () {var arr [6]int = [6]int{1, 9, 8, $, 0, 2}arrsize: = Len (arr) for i: =; i < arrsize; i++ {//fmt. PRINTLN (arr) for j: = i; J < Arrsize; J + + {if arr[i] < Arr[j] {tmp: = Arr[i]arr[i] = arr[j]arr[j] = tmp}}}fmt. Println (ARR)}
Problems caused by 3.slice expansion:
Data structures for slices:
Slice in the expansion will generate a new slice, and then copy the original slice to the new slice, and then append. This causes the address of the element to change.
Example:
Func main () {slice: = make ([]int, 2, 5) slice[0] = 10slice[1] = 20fmt. Printf ("%p\n", slice) slice = append (slice, 1, 2, 5, 6) fmt. Printf ("%p", Slice)}
Because of the expansion, the address of the slice array printed two times is inconsistent.
Slice as a function parameter, we pass a reference, we append an element and expect to get the slice of the added element, but in fact it does not:
Func main () {slice: = make ([]int, 0) changeslice (slice) fmt. Println (Slice)}func changeslice (Slice []int) {slice = append (slice, 10)}//output: [], does not add element this is because the tile expansion, resulting in the array address has changed.
Therefore, you should change the above code to return slice and assign the value:
Func main () {slice: = make ([]int, 0) slice = changeslice (slice) fmt. Println (Slice)}func changeslice (Slice []int) []int {slice = append (slice, ten) return slice}
An example of a 4.switch type:
Type Phone Interface {call ()}type android struct {name String}func (Android *android) call () {FMT. Println (Android.name)}func main () {android: = &android{name: "LG"}hangup (Android)}func Hangup (all Phone) {Switch V: = All. (type) {Case *android:fmt. Println ("Hangup", V.name) default:fmt. PRINTLN ("Unknow Device")}}
Using assertions:
Func hangup (All phones) {if a, OK: = All. ( *android); OK {fmt. Println (A.name) return}fmt. PRINTLN ("Unknow Device")}
5.fallthrough:
Fallthrough enforces the following case code, Fallthrough does not determine if the result of expr for the next scenario is true
Func Main () {A: = 1switch a {case 1:fmt. Println ("1") Fallthroughcase 2:fmt. Println ("2") default:fmt. Println ("no match")}}//Output 1 2, the second case does not determine whether a is equal to 2
6.goroutine Sync: Sync
Import ("FMT" "Runtime" "Sync") func main () {runtime. Gomaxprocs (runtime. NUMCPU ()) WG: = Sync. WAITGROUP{}WG. ADD (+) for I: = 0; I < 10; i++ {Go work (&WG, i)}wg. Wait ()}func work (WG *sync. Waitgroup, index int) {FMT. PRINTLN (Index) WG. Done ()}