go - fragment

判断一个 struct 是否实现了某个 interface

1
2
3
4
5
6
7
8
9
10
type IFace iterface {
Eat() string
}

type Bird struct {
Eyes string
Color int
}

var _ IFace = new(Bird)

判断一个数是否为2的n次方

1
2
3
4
// IsPowerOfTwo reports whether given integer is a power of two.
func IsPowerOfTwo(n int) bool {
return n&(n-1) == 0
}

print 文件调用文件的路径

1
2
log.SetFlags(log.Llongfile)
log.Println(111)

How to best clear a slice: empty vs. nil

  • yourbasic.org/golang

Remove all elements

  • To remove all elements, simply set the slice to nil.

    1
    2
    3
    a := []string{"A", "B", "C", "D", "E"}
    a = nil
    fmt.Println(a, len(a), cap(a)) // [] 0 0
  • This will release the underlying array to the garbage collector (assuming there are no other references).

Keep allocated memory

  • To keep the underlying array, slice the slice to zero length.
    1
    2
    3
    a := []string{"A", "B", "C", "D", "E"}
    a = a[:0]
    fmt.Println(a, len(a), cap(a)) // [] 0 5
  • If the slice is extended again, the original data reappears.
    1
    fmt.Println(a[:2]) // [A B]

Empty slice vs. nil slice

  • In practice, nil slices and empty slices can often be treated in the same way:

    • they can be used with the same effect in for loops and append functions,
    • they have zero length and capacity,
    • and they even look the same when printed.
      1
      2
      3
      4
      var a []int = nil
      fmt.Println(len(a)) // 0
      fmt.Println(cap(a)) // 0
      fmt.Println(a) // []
  • However, if needed, you can tell the difference.

    1
    2
    3
    4
    5
    6
    7
    8
    var a []int = nil
    var a0 []int = make([]int, 0)

    fmt.Println(a == nil) // true
    fmt.Println(a0 == nil) // false

    fmt.Printf("%#v\n", a) // []int(nil)
    fmt.Printf("%#v\n", a0) // []int{}
  • The official Go wiki recommends using nil slices over empty slices.

[…] the nil slice is the preferred style.

Note that there are limited circumstances where a non-nil but zero-length slice is preferred, such as when encoding JSON objects (a nil slice encodes to null, while []string{} encodes to the JSON array []).

When designing interfaces, avoid making a distinction between a nil slice and a non-nil, zero-length slice, as this can lead to subtle programming errors.
The Go wiki: Declaring empty slices