【Go Programming Language 2】express
一、In Go, the sign of the remainder is always the same as the sign of the dividend, so -5%3 and -5%-3 are both -2. The behavior of / depends on whether its operands are integers, so 5.0/4.0 is 1.25, but 5/4 is 1 because integerapp
division truncates the result toward zero.ide
二、bitwise operator函數
The &^ op erator is bit cle ar (AND NOT): in the expression z = x &^ y, each bit of z is 0 if the corresponding bit of y is 1; otherwise it equals the corresponding bit of x.ui
三、Printf 的高級用法。this
the [1] ‘‘adverbs’’ after % tell Printf to use the first operand over and over again. Second, the # adverb for %o or %x or %X tells Printf to emit a 0 or 0x or 0X prefix respectively.lua
四、Runes are print ed wit h %c, or with %q if quoting is desired: spa
五、math.NaN()code
Any comparison wit h NaN always yields false:component
六、Complex Numbers
3.141592i & 2i is an im aginary literal, denoting a complex number wit ha zero real component:
complex literal:
七、strings
Either or both of the i and j operands may be omitted, in which case the defau lt values of 0 (the start of the string) and len(s) (its end) are assumed, respectively.
Since strings are immutable, constructions that try to modify a string's data in place are not allowed:
A raw string literal is written `...`, using backquotes instead of double quotes.
八、UTF-8
The high-order bits of the first byte of the encoding for a rune indicate how many bytes follow. A high-order indicates 7-bit ASCII.
No rune’s encoding is a substring of any other, or even of a sequence of others, so you can search for a rune by just searching for its bytes.
The unicode package provides functions for working with individual runes , and the unicode/utf8 package provides functions for encoding and decoding runes as bytes using UTF-8.
utf8.RuneCountInString() 能夠獲取一個 utf8 字符串中的字符個數。
utf8.DecodeRuneInString(str) 能夠解析出第一個utf8字符,以下:
使用 range 遍歷utf8字符串,range會自動調用 DecodeRuneInString()方法進行解碼。
所以,能夠使用 range 來統計字符串長度。下面右圖是左圖的精簡版,忽略了 range 的返回值。
也能夠調用 utf8.RuneCountInString(s) 來得到 utf8 字符串的長度。
[]rune(utf8_str) 能夠返回 utf8 字符串的 unicode code point.
九、Because strings are immutable, building up strings incrementally can involve a lot of allocation and copying . In such cases, it's more efficient to use the type bytes.Buffer.
basename() 移除路徑、以及擴展名。
十、A string contains an array of bytes that, once created, is immutable. By contrast, the elements of a byte slice can be freely modified.
Strings can be converted to byte slices and back again:
十一、strconv.Itoa() 用於轉換。
十二、FormatInt and FormatUint can be used to format numbers in a different base.
fmt.Printf verbs %b, %d, %u, and %x are often more convenient than Format functions,especially if we want to include additional information besides the number:
strconv.Atoi()、strcov.ParseInt() 用於將str 轉換爲 int。
1三、單個常量
多個常量
1四、const generator iota
Below declares Sunday to be 0, Monday to be 1, and so on.
As iota increments, each constant is assigned the value of 1 << iota, which evaluates to successive powers of two, each corresponding to a single bit.
1五、Untyped Constants
many constants are not committed to a particular type.
The compiler represents these uncommitted constants with much greater numeric precision than values of basic types, and arithmetic on them is more precise than machine arithmetic; you may assume at least 256 bits of precision.
1六、Arrays are homogeneous—their elements all have the same type—whereas structs are heterogeneous. Both arrays and structs are fixed size. In contrast, slices and maps are dynamic data structures that grow as values are added.
Array 中全部元素必須有相關的 type。Array、struct 固定大小,slice、map 動態大小。
1七、使用 array literal 初始化 array.
上述代碼,兩個3重複了。因此也能夠使用 := 以及 ... 來編譯器推導array 長度。
[3]int and [4]int are different types.
array literal 中能夠設定索引來定義。
Below defines an array r with 100 elements, all zero except for the last, which has value -1.
1八、array 的相等性比較,使用的是其內全部元素的相等性比較。
1九、Slice
Slices represent variable-length sequences whose elements all have the same type. A slice type is written []T, where the elements have type T; it looks like an array type without a size.
Slicing beyond cap(s) causes a panic, but slicing beyond len(s) extends the slice, so the result may be longer than the original.
Passing a slice to a function permits the function to modify the underlying array elements.
經過 slice 能夠修改 array中的元素。
20、A simple way to rotate a slice left by n elements is to apply the function three times, reverse first to the leading n elements, then to the remaining elements, and finally to the whole slice.
Unlike arrays, slices are not comparable, so we cannot use == to test whether two slices contain the same elements. The only legal slice comp arison is against nil, as in:
注意區分 slice 的定義和建立。定義一個slice,其值爲nil;建立一個slice,其值爲非nil。
The built-in function make creates a slice of a specified element type, length, and capacity。
2一、內置的 append(slice, value) 函數有可能會建立一個全新的slice。
1 func appendInt(x []int, y int) []int{ 2 var z[]int 3 zlen:=len(x)+1 4 if zlen<=cap(x){ 5 z = x[:zlen] 6 }else{ 7 zcap:=zlen 8 if zcap<2*len(x){ 9 zcap=2*len(x) 10 } 11 z=make([]int,zlen,zcap) 12 copy(z,x) 13 } 14 z[len(x)]=y 15 return z 16 }
因數調用 append() 後,有可能會建立一個全新的 slice,因此一般會將 append() 函數賦值給回本身。以下:
append() 能夠添加超過一個元素。
2二、slice 能夠理解爲以下結構
2三、使用 ellipsis 能夠實現變長參數。如:
2四、In-Place Slice Techniques
1)nonempty
2)
2五、slice can be used to implement a stack.
stack = append(stack, v) // push v top:=stack[len(stack)-1] // top of stack stack = stack[:len(stack)-1] // pop
實現從 slice 中移除元素。
And if we don’t need to preserve the order, we can just mov e the last element int o the gap:
2六、maps
In Go, a map is a reference to a hash table, and a map type is written map[K]V, where K and V are the types of its keys and values. All of the keys in a given map are of the same type, and all of the values are of the same type, but the keys need not be of the same type as the values.
map literal:
delete() 函數能夠移除key。
But a map element is not a var iable, and we cannot take its address:
map[] 方法實際上返回兩個元素,第二個元素指明本元素是否有值。
2七、Go does not provide a set type, but since the keys of a map are distinct, a map can serve this purpose.
一個map的value能夠是另外一個map。
2八、
2九、
30、