Go
言語で文字列を数値に、数値を文字列に変換する方法を説明していきます。
strconv
文字列と数値を相互に変換するにあたり、「strconv」というパッケージをインポートする必要があります。
strconv package - strconv - Go Packages
Package strconv implements conversions to and from string representations of basic data types.
import "strconv"
数値を文字列に変換する
import "strconv"
i := 8888
s := strconv.Itoa(i)
fmt.PrintLn(s)
// "8888"
文字列を数値に変換する
import "strconv"
s := "8888"
i := strconv.Atoi(s)
fmt.PrintLn(i)
// 8888
最後に
Go
言語の勉強始めたばかりなので、文字列⇔数値の変換はしっかり覚えておきたいですね。
コメント