github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xstrings/join.go (about) 1 package xstrings 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 8 "github.com/go-board/x-go/xcodec" 9 ) 10 11 func JoinInt(i []int, sep string) string { 12 buf := strings.Builder{} 13 for _, v := range i { 14 if buf.Len() > 0 { 15 buf.WriteString(sep) 16 } 17 buf.WriteString(strconv.FormatInt(int64(v), 10)) 18 } 19 20 return buf.String() 21 } 22 23 func JoinInt8(i []int8, sep string) string { 24 buf := strings.Builder{} 25 for _, v := range i { 26 if buf.Len() > 0 { 27 buf.WriteString(sep) 28 } 29 buf.WriteString(strconv.FormatInt(int64(v), 10)) 30 } 31 32 return buf.String() 33 } 34 35 func JoinInt16(i []int16, sep string) string { 36 buf := strings.Builder{} 37 for _, v := range i { 38 if buf.Len() > 0 { 39 buf.WriteString(sep) 40 } 41 buf.WriteString(strconv.FormatInt(int64(v), 10)) 42 } 43 44 return buf.String() 45 } 46 47 func JoinInt32(i []int32, sep string) string { 48 buf := strings.Builder{} 49 for _, v := range i { 50 if buf.Len() > 0 { 51 buf.WriteString(sep) 52 } 53 buf.WriteString(strconv.FormatInt(int64(v), 10)) 54 } 55 56 return buf.String() 57 } 58 59 func JoinInt64(i []int64, sep string) string { 60 buf := strings.Builder{} 61 for _, v := range i { 62 if buf.Len() > 0 { 63 buf.WriteString(sep) 64 } 65 buf.WriteString(strconv.FormatInt(v, 10)) 66 } 67 68 return buf.String() 69 } 70 71 func JoinUint(i []uint, sep string) string { 72 buf := strings.Builder{} 73 for _, v := range i { 74 if buf.Len() > 0 { 75 buf.WriteString(sep) 76 } 77 buf.WriteString(strconv.FormatUint(uint64(v), 10)) 78 } 79 80 return buf.String() 81 } 82 83 func JoinUint8(i []uint8, sep string) string { 84 buf := strings.Builder{} 85 for _, v := range i { 86 if buf.Len() > 0 { 87 buf.WriteString(sep) 88 } 89 buf.WriteString(strconv.FormatUint(uint64(v), 10)) 90 } 91 92 return buf.String() 93 } 94 95 func JoinUint16(i []uint16, sep string) string { 96 buf := strings.Builder{} 97 for _, v := range i { 98 if buf.Len() > 0 { 99 buf.WriteString(sep) 100 } 101 buf.WriteString(strconv.FormatUint(uint64(v), 10)) 102 } 103 104 return buf.String() 105 } 106 107 func JoinUint32(i []uint32, sep string) string { 108 buf := strings.Builder{} 109 for _, v := range i { 110 if buf.Len() > 0 { 111 buf.WriteString(sep) 112 } 113 buf.WriteString(strconv.FormatUint(uint64(v), 10)) 114 } 115 116 return buf.String() 117 } 118 119 func JoinUint64(i []uint64, sep string) string { 120 buf := strings.Builder{} 121 for _, v := range i { 122 if buf.Len() > 0 { 123 buf.WriteString(sep) 124 } 125 buf.WriteString(strconv.FormatUint(v, 10)) 126 } 127 128 return buf.String() 129 } 130 131 func JoinStringer(i []fmt.Stringer, sep string) string { 132 buf := strings.Builder{} 133 for _, v := range i { 134 if buf.Len() > 0 { 135 buf.WriteString(sep) 136 } 137 buf.WriteString(v.String()) 138 } 139 140 return buf.String() 141 } 142 143 func JoinAny(i []interface{}, sep string, codec xcodec.Codec) (string, error) { 144 buf := strings.Builder{} 145 for _, v := range i { 146 if buf.Len() > 0 { 147 buf.WriteString(sep) 148 } 149 bytes, err := codec.Marshal(v) 150 if err != nil { 151 return "", err 152 } 153 buf.Write(bytes) 154 } 155 156 return buf.String(), nil 157 }