From 0c41cd5bcfcc69083bd63d2fa68c0bc9161fc6bf Mon Sep 17 00:00:00 2001 From: xing Date: Fri, 16 Sep 2022 18:21:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=8B=BC=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper/func.go | 14 ++++++++++++++ helper/func_test.go | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/helper/func.go b/helper/func.go index f182d1b..848a6e2 100644 --- a/helper/func.go +++ b/helper/func.go @@ -2,6 +2,7 @@ package helper import ( "reflect" + "strings" ) func IsContainInArr[T comparable](a T, arr []T) bool { @@ -31,3 +32,16 @@ func RangeSlice[T ~int | ~uint | ~int64 | ~int8 | ~int16 | ~int32 | ~uint64](sta } return r } + +func StrJoin(s ...string) (str string) { + if len(s) == 1 { + return s[0] + } else if len(s) > 1 { + b := strings.Builder{} + for _, s2 := range s { + b.WriteString(s2) + } + str = b.String() + } + return +} diff --git a/helper/func_test.go b/helper/func_test.go index 8783794..f58c5fc 100644 --- a/helper/func_test.go +++ b/helper/func_test.go @@ -112,3 +112,23 @@ func TestRangeSlice(t *testing.T) { }) } } + +func TestStrJoin(t *testing.T) { + type args struct { + s []string + } + tests := []struct { + name string + args args + wantStr string + }{ + {name: "t1", args: args{s: []string{"a", "b", "c"}}, wantStr: "abc"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if gotStr := StrJoin(tt.args.s...); gotStr != tt.wantStr { + t.Errorf("StrJoin() = %v, want %v", gotStr, tt.wantStr) + } + }) + } +}