中條 美冬

1640000843

Golangでint値を文字列に変換する方法

Golangには、intを文字列に変換する関数を提供するstrconv という組み込みパッケージがあります。int型をstring型に変換するには、次のいずれかの方法を使用できます。

  1. strconv.FormatInt:FormatIntは、2 <= base <= 36の場合、指定された基数のiの文字列表現を返します。結果では、数字> = 10の場合は小文字の「a」から「z」が使用されます。
  2. strconv.Itoa:ItoaはFormatInt(int64(i)、10)の省略形です。

Golang int / int64から文字列

Go(Golang)は、標準ライブラリstrconvからのパッケージから直接文字列と整数の変換を提供します。

Golangで整数値を文字列に変換するには、strconvパッケージのFormatInt関数を使用できます。

func FormatInt(i int64、base int)文字列

Golang FormatInt()関数は、2 <= base <= 36の場合、指定された基数のiの文字列表現を返します。最終結果では、数字> = 10の場合は小文字の「a」から「z」を使用します。

// hello.go

package main

import (
    "fmt"
    "reflect"
    "strconv"
)

func main() {

    info := 1121
    /** converting the info variable into a string using FormatInt method */
    str2 := strconv.FormatInt(int64(info), 10)
    fmt.Println(str2)
    fmt.Println(reflect.TypeOf(str2))
}

出力

go run hello.go
1121
string

私たちは、使用している reflect 使用してパッケージを Typeof() データ型を決定するための方法を、それが文字列です。 Golang reflectパッケージには、変数のタイプを検査するためのメソッドがあります。次に、FormatInt()メソッドを使用してGolangintを文字列に変換しました。

func Itoa(i int)文字列

Golang strconv.Itoa()は、FormatInt(int64(i)、10)と同等です。

これらの関数を使用して整数をASCII文字列に変換する方法の例を見てみましょう。

// hello.go

package main

import (
    "fmt"
    "reflect"
    "strconv"
)

func main() {

    info := 1121
    fmt.Println(reflect.TypeOf(info))
    /** converting the info variable into a string using FormatInt method */
    str2 := strconv.Itoa(info)
    fmt.Println(str2)
    fmt.Println(reflect.TypeOf(str2))
}

出力

go run hello.go
int
1121
string

単純な変換では、値はUnicodeコードポイントとして解釈され、結果の文字列には、UTF-8でエンコードされたそのコードポイントによって表される文字が含まれます。

次のコードを参照してください。

package main

import (
    "fmt"
    "reflect"
)

func main() {

    s := 97
    fmt.Println(reflect.TypeOf(s))
    /** converting the info variable into a string using FormatInt method */
    str2 := string(97)
    fmt.Println(str2)
    fmt.Println(reflect.TypeOf(str2))
}

出力

go run hello.go
int
a
string

結論

Golang strconvパッケージはFormInt()Itoa()go integerをstringに変換する関数を提供し、refectパッケージを使用してデータ型を検証できます。

What is GEEK

Buddha Community

中條 美冬

1640000843

Golangでint値を文字列に変換する方法

Golangには、intを文字列に変換する関数を提供するstrconv という組み込みパッケージがあります。int型をstring型に変換するには、次のいずれかの方法を使用できます。

  1. strconv.FormatInt:FormatIntは、2 <= base <= 36の場合、指定された基数のiの文字列表現を返します。結果では、数字> = 10の場合は小文字の「a」から「z」が使用されます。
  2. strconv.Itoa:ItoaはFormatInt(int64(i)、10)の省略形です。

Golang int / int64から文字列

Go(Golang)は、標準ライブラリstrconvからのパッケージから直接文字列と整数の変換を提供します。

Golangで整数値を文字列に変換するには、strconvパッケージのFormatInt関数を使用できます。

func FormatInt(i int64、base int)文字列

Golang FormatInt()関数は、2 <= base <= 36の場合、指定された基数のiの文字列表現を返します。最終結果では、数字> = 10の場合は小文字の「a」から「z」を使用します。

// hello.go

package main

import (
    "fmt"
    "reflect"
    "strconv"
)

func main() {

    info := 1121
    /** converting the info variable into a string using FormatInt method */
    str2 := strconv.FormatInt(int64(info), 10)
    fmt.Println(str2)
    fmt.Println(reflect.TypeOf(str2))
}

出力

go run hello.go
1121
string

私たちは、使用している reflect 使用してパッケージを Typeof() データ型を決定するための方法を、それが文字列です。 Golang reflectパッケージには、変数のタイプを検査するためのメソッドがあります。次に、FormatInt()メソッドを使用してGolangintを文字列に変換しました。

func Itoa(i int)文字列

Golang strconv.Itoa()は、FormatInt(int64(i)、10)と同等です。

これらの関数を使用して整数をASCII文字列に変換する方法の例を見てみましょう。

// hello.go

package main

import (
    "fmt"
    "reflect"
    "strconv"
)

func main() {

    info := 1121
    fmt.Println(reflect.TypeOf(info))
    /** converting the info variable into a string using FormatInt method */
    str2 := strconv.Itoa(info)
    fmt.Println(str2)
    fmt.Println(reflect.TypeOf(str2))
}

出力

go run hello.go
int
1121
string

単純な変換では、値はUnicodeコードポイントとして解釈され、結果の文字列には、UTF-8でエンコードされたそのコードポイントによって表される文字が含まれます。

次のコードを参照してください。

package main

import (
    "fmt"
    "reflect"
)

func main() {

    s := 97
    fmt.Println(reflect.TypeOf(s))
    /** converting the info variable into a string using FormatInt method */
    str2 := string(97)
    fmt.Println(str2)
    fmt.Println(reflect.TypeOf(str2))
}

出力

go run hello.go
int
a
string

結論

Golang strconvパッケージはFormInt()Itoa()go integerをstringに変換する関数を提供し、refectパッケージを使用してデータ型を検証できます。