LoginSignup
21
6

More than 3 years have passed since last update.

【Go】string型からint64型・uint64型へ変換する方法

Last updated at Posted at 2020-01-09

string型からint64型へ変換する方法

// int64
var strInt64 string = "9223372036854775807"

// strconv.ParseInt(文字列, 基数(10進数),ビット長)
convertedStrInt64, _ := strconv.ParseInt(strInt64, 10, 64)

9223372036854775807int64の最大数です

int64 is the set of all signed 64-bit integers. Range: -9223372036854775808 through 9223372036854775807.

string型からuint64型へ変換する方法

// uint64   
var strUint64 string = "18446744073709551615"

// strconv.ParseUint(文字列, 基数(10進数),ビット長)
convertedStrUint64, _ := strconv.ParseUint(strUint64, 10, 64)

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() {
    // 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))
}

実行結果

9223372036854775807 with type int64 
18446744073709551615 with type uint64 

参考URL

21
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
6