int64型からstring型へ変換する方法
// int64
var Int64 int64 = 9223372036854775807
// strconv.FormatInt(int64, 基数(10進数))
convertedInt64 := strconv.FormatInt(Int64, 10)
9223372036854775807
はint64
の最大数です
int64 is the set of all signed 64-bit integers. Range: -9223372036854775808 through 9223372036854775807.
uint64型からstring型へ変換する方法
// uint64
var Uint64 uint64 = 18446744073709551615
// strconv.FormatUint(uint64, 基数(10進数))
convertedUint64 := strconv.FormatUint(Uint64, 10)
18446744073709551615
はuint64の最大数です
uint64 is the set of all unsigned 64-bit integers. Range: 0 through 18446744073709551615.
Go Playground
package main
import (
"strconv"
"fmt"
"reflect"
)
func main() {
var Int64 int64 = 9223372036854775807
convertedInt64 := strconv.FormatInt(Int64,10)
fmt.Printf("%v with type %s \n", convertedInt64, reflect.TypeOf(convertedInt64))
var Uint64 uint64 = 18446744073709551615
convertedUint64 := strconv.FormatUint(Uint64,10)
fmt.Printf("%v with type %s \n", convertedUint64, reflect.TypeOf(convertedUint64))
}
実行結果
9223372036854775807 with type string
18446744073709551615 with type string
【おまけ】string型からint64・uint64型へ変換からの再びstring型へ変換
package main
import (
"strconv"
"fmt"
"reflect"
)
func main() {
fmt.Println("\n----- convert String to Int64 and Uint64 -------------------")
// int64
var strInt64 string = "9223372036854775807"
convertedStrInt64, _ := strconv.ParseInt(strInt64, 10, 64)
fmt.Printf("%v with type %s \n", convertedStrInt64, reflect.TypeOf(convertedStrInt64))
// uint64
var strUint64 string = "18446744073709551615"
convertedStrUint64, _ := strconv.ParseUint(strUint64, 10, 64)
fmt.Printf("%v with type %s \n", convertedStrUint64, reflect.TypeOf(convertedStrUint64))
fmt.Println("\n----- convert Int64 and Uint64 to String -------------------")
convertedInt64 := strconv.FormatInt(convertedStrInt64,10)
fmt.Printf("%v with type %s \n", convertedInt64, reflect.TypeOf(convertedInt64))
convertedUint64 := strconv.FormatUint(convertedStrUint64,10)
fmt.Printf("%v with type %s \n", convertedUint64, reflect.TypeOf(convertedUint64))
}
実行結果
----- convert String to Int64 and Uint64 -------------------
9223372036854775807 with type int64
18446744073709551615 with type uint64
----- convert Int64 and Uint64 to String -------------------
9223372036854775807 with type string
18446744073709551615 with type string