github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/types/README.md (about)

     1  # Types
     2  
     3  拓展类型定义,方便函数的定义,以及自定义方法,用户可以直接使用。
     4  
     5  ## 基本类型
     6  ```go
     7  type Interface interface{}
     8  
     9  type Byte byte
    10  
    11  type Int int
    12  type Int64 int
    13  type Int32 int32
    14  type Int16 int16
    15  type Int8 int8
    16  
    17  type Uint uint
    18  type Uint64 uint
    19  type Uint32 uint32
    20  type Uint16 uint16
    21  type Uint8 uint8
    22  
    23  type String string
    24  
    25  type Float64 float64
    26  type Float32 float32
    27  
    28  type Bool bool
    29  
    30  type Any = interface{}
    31  
    32  type AnySlice = []Any
    33  type InterfaceSlice = AnySlice
    34  
    35  type ByteSlice = []byte
    36  
    37  type IntSlice = []int
    38  type Int64Slice = []int64
    39  type Int32Slice = []int32
    40  type Int16Slice = []int16
    41  type Int8Slice = []int8
    42  
    43  type UintSlice = []uint
    44  type Uint64Slice = []uint64
    45  type Uint32Slice = []uint32
    46  type Uint16Slice = []uint16
    47  type Uint8Slice = []uint8
    48  
    49  type StringSlice = []string
    50  
    51  type Float64Slice = []float64
    52  type Float32Slice = []float32
    53  
    54  type BoolSlice = []bool
    55  ```
    56  ## Comparable 比较的接口
    57  > 未来`go`[proposal: spec: generic programming facilities](https://github.com/golang/go/issues/15292)引入泛型可以解决这个问题,但是很遗憾,可见的时间内不会有泛型
    58  ```go
    59  // Ordering is the result of a comparison between two values.
    60  type Ordering int
    61  
    62  const (
    63      // 小于
    64      OrderingLess    Ordering = -1
    65      // 等于
    66      OrderingEqual            = 0
    67      // 大于
    68      OrderingGreater          = 1
    69  )
    70  
    71  type Comparable interface{ Compare(o Comparable) Ordering }
    72  ```
    73  > 目前上面定义的基本类型已经实现了 `Comparable`
    74  > 同时[xsort](../xsort/README.md)也使用到了`Comparable`
    75  ## ID
    76  ```go
    77  type ID uint64
    78  // 大端顺序的二进制结果
    79  func (id ID) Binary() []byte
    80  // 大端顺序的二进制结果的16进制的结果
    81  func (id ID) Hex() string
    82  // 大端顺序的二进制结果的base64的结果
    83  func (id ID) Base64() string
    84  ```
    85  
    86  ## Convert 类型转换
    87  ```go
    88  // int 数组到int64数组的转换
    89  func IntSliceToInt64Slice(a types.IntSlice) types.Int64Slice
    90  // int64 数组到int数组的转换
    91  func Int64SliceToIntSlice(a types.Int64Slice) types.IntSlice
    92  // int 数组到interface{}数组的转换
    93  func IntSliceToAnySlice(a types.IntSlice) types.AnySlice
    94  // int64 数组到interface{}数组的转换
    95  func Int64SliceToAnySlice(a types.Int64Slice) types.AnySlice
    96  // int 数组到string数组的转换
    97  func IntSliceToStringSlice(a types.IntSlice) types.StringSlice
    98  // int64 数组到string数组的转换
    99  func Int64SliceToStringSlice(a types.Int64Slice) types.StringSlice
   100  // string 数组到int数组的转换
   101  func StringSliceToIntSlice(a types.StringSlice) (b types.IntSlice, hasErr bool)
   102  // string 数组到int64数组的转换
   103  func StringSliceToInt64Slice(a types.StringSlice) (b types.Int64Slice, hasErr bool)
   104  ```