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

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

int from string in go

问题

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

i := strconv.Atoi("10")

英文:

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

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

一些其他选项:

package main
import "fmt"

func main() {
   var n int
   fmt.Sscan("100", &n)
   fmt.Println(n == 100)
}
package main
import "strconv"

func main() {
   n, e := strconv.ParseInt("100", 10, 64)
   if e != nil {
      panic(e)
   }
   println(n == 100)
}
英文:

Some other options:

package main
import "fmt"

func main() {
   var n int
   fmt.Sscan("100", &n)
   fmt.Println(n == 100)
}
package main
import "strconv"

func main() {
   n, e := strconv.ParseInt("100", 10, 64)
   if e != nil {
      panic(e)
   }
   println(n == 100)
}

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:

确定