在Go语言中将字符串转换为整数

huangapple go评论132阅读模式
英文:

int from string in go

问题

什么是从字符串创建整数值的函数?

i := strconv.Atoi("10")

英文:

What's the function to create a int value from string

  1. i := ???.????( "10" )

答案1

得分: 8

导入strconv包(src/pkg/strconv),然后使用strconv.Atoi("10")

英文:

Import the strconv package (src/pkg/strconv), then use strconv.Atoi("10")

答案2

得分: 0

一些其他选项:

  1. package main
  2. import "fmt"
  3. func main() {
  4. var n int
  5. fmt.Sscan("100", &n)
  6. fmt.Println(n == 100)
  7. }
  1. package main
  2. import "strconv"
  3. func main() {
  4. n, e := strconv.ParseInt("100", 10, 64)
  5. if e != nil {
  6. panic(e)
  7. }
  8. println(n == 100)
  9. }
英文:

Some other options:

  1. package main
  2. import "fmt"
  3. func main() {
  4. var n int
  5. fmt.Sscan("100", &n)
  6. fmt.Println(n == 100)
  7. }
  1. package main
  2. import "strconv"
  3. func main() {
  4. n, e := strconv.ParseInt("100", 10, 64)
  5. if e != nil {
  6. panic(e)
  7. }
  8. println(n == 100)
  9. }

huangapple
  • 本文由 发表于 2010年4月20日 14:14:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/2672948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定