github.com/mymmsc/gox@v1.3.33/util/util_string.go (about)

     1  // Copyright (c) 2015, Emir Pasic. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package utils provides common utility functions.
     6  //
     7  // Provided functionalities:
     8  // - sorting
     9  // - comparators
    10  package util
    11  
    12  import (
    13  	"fmt"
    14  	"strconv"
    15  )
    16  
    17  // ToString converts a value to string.
    18  func ToString(value interface{}) string {
    19  	switch value := value.(type) {
    20  	case string:
    21  		return value
    22  	case int8:
    23  		return strconv.FormatInt(int64(value), 10)
    24  	case int16:
    25  		return strconv.FormatInt(int64(value), 10)
    26  	case int32:
    27  		return strconv.FormatInt(int64(value), 10)
    28  	case int64:
    29  		return strconv.FormatInt(int64(value), 10)
    30  	case uint8:
    31  		return strconv.FormatUint(uint64(value), 10)
    32  	case uint16:
    33  		return strconv.FormatUint(uint64(value), 10)
    34  	case uint32:
    35  		return strconv.FormatUint(uint64(value), 10)
    36  	case uint64:
    37  		return strconv.FormatUint(uint64(value), 10)
    38  	case float32:
    39  		return strconv.FormatFloat(float64(value), 'g', -1, 64)
    40  	case float64:
    41  		return strconv.FormatFloat(float64(value), 'g', -1, 64)
    42  	case bool:
    43  		return strconv.FormatBool(value)
    44  	default:
    45  		return fmt.Sprintf("%+v", value)
    46  	}
    47  }