github.com/erda-project/erda-infra@v1.0.9/pkg/strutil/interface.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package strutil 16 17 import ( 18 "fmt" 19 "strconv" 20 ) 21 22 // String convert interface to string 23 func String(i interface{}) string { 24 if i == nil { 25 return "" 26 } 27 switch v := i.(type) { 28 case int: 29 return strconv.Itoa(v) 30 case int8: 31 return strconv.FormatInt(int64(v), 10) 32 case int32: 33 return strconv.FormatInt(int64(v), 10) 34 case int64: 35 return strconv.FormatInt(v, 10) 36 case uint: 37 return strconv.FormatUint(uint64(v), 10) 38 case uint8: 39 return strconv.FormatUint(uint64(v), 10) 40 case uint32: 41 return strconv.FormatUint(uint64(v), 10) 42 case uint64: 43 return strconv.FormatUint(v, 10) 44 case float32: 45 return strconv.FormatFloat(float64(v), 'f', -1, 32) 46 case float64: 47 return strconv.FormatFloat(v, 'f', -1, 64) 48 case []byte: 49 return string(v) 50 case string: 51 return v 52 default: 53 return fmt.Sprintf("%v", i) 54 } 55 }