Go Programming Language 2

1、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 integer

division truncates the result toward zero.

2、bitwise operator

  Go Programming Language 2

  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.

3、Printf 的高级用法。

  Go Programming Language 2

  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.

4、Runes are print ed wit h %c, or with %q if quoting is desired: 

  Go Programming Language 2

5、math.NaN()

  Any comparison wit h NaN always yields false:

  Go Programming Language 2

6、Complex Numbers

  Go Programming Language 2

  3.141592i  & 2i is an im aginary literal, denoting a complex number wit ha zero real component:

  complex literal:

  Go Programming Language 2

7、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.

  Go Programming Language 2

  Since strings are immutable, constructions that try to modify a string's data in place are not allowed:

  Go Programming Language 2

  A raw string literal is written `...`, using backquotes instead of double quotes.

8、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.

  Go Programming Language 2

  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 字符串中的字符个数。

  Go Programming Language 2

  utf8.DecodeRuneInString(str) 可以解析出第一个utf8字符,如下:

  Go Programming Language 2

  使用 range 遍历utf8字符串,range会自动调用 DecodeRuneInString()方法进行解码。

  Go Programming Language 2Go Programming Language 2

  因此,可以使用 range 来统计字符串长度。下面右图是左图的精简版,忽略了 range 的返回值。

  Go Programming Language 2Go Programming Language 2

  也可以调用 utf8.RuneCountInString(s) 来获得 utf8 字符串的长度。

  []rune(utf8_str) 可以返回 utf8 字符串的 unicode code point.

  Go Programming Language 2

9、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() 移除路径、以及扩展名。

    Go Programming Language 2

10、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:

  Go Programming Language 2

11、strconv.Itoa() 用于转换。

  Go Programming Language 2  

12、FormatInt and FormatUint can be used to format numbers in a different base.

  Go Programming Language 2

  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:

  Go Programming Language 2

  strconv.Atoi()、strcov.ParseInt() 用于将str 转换为 int。

  Go Programming Language 2

13、单个常量

  Go Programming Language 2

  多个常量

  Go Programming Language 2

14、const generator iota

  Below declares Sunday to be 0, Monday to be 1, and so on.

  Go Programming Language 2

  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.

  Go Programming Language 2

15、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.

16、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 动态大小。

17、使用 array literal 初始化 array.

  Go Programming Language 2

  上述代码,两个3重复了。所以也可以使用 := 以及 ... 来编译器推导array 长度。

  Go Programming Language 2

  [3]int and [4]int are different types. 

  Go Programming Language 2

  array literal 中可以设定索引来定义。

  Go Programming Language 2

  Below defines an array r with 100 elements, all zero except for the last, which has value -1.

  Go Programming Language 2

18、array 的相等性比较,使用的是其内所有元素的相等性比较。

  Go Programming Language 2

19、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中的元素。

  Go Programming Language 2

  Go Programming Language 2

20、A simple way to rotate a slice left by n elements is to apply the function three times, first to the leading n elements, then to the remaining elements, and finally to the whole slice.

    Go Programming Language 2

  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:

  Go Programming Language 2

  注意区分 slice 的定义和创建。定义一个slice,其值为nil;创建一个slice,其值为非nil。

  Go Programming Language 2

  The built-in function make creates a slice of a specified element type, length, and capacity。

  Go Programming Language 2

21、内置的 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 }
View Code

相关文章: