본문 바로가기
Go/Packages

[Go] Package strconv

by llHoYall 2020. 10. 26.

Package strconv implements conversions to and from string representations of basic data types.

Methods

Atoi

Atoi equivalent to ParseInt(s, 10, 0), converted to type int.

func Atoi(s string) (int, error)

Itoa

Itoa is equivalent to FormatInt.

func Itoa(i int) string

ParseBool, ParseUInt, ParseInt, ParseFloat

ParseBool, ParseUInt, ParseInt, and ParseFloat convert strings to values.

func ParseBool(s string) (bool, error)
func ParseUint(s string, base int, bitSize int) (uint64, error)
func ParseInt(s string, base int, bitSize int) (int64, error)
func ParseFloat(s string, bitSize int) (float64, error)

Usages

package main

import (
  "fmt"
  "log"
  "strconv"
)

func main() {
  iStr := "7"
  sInt := 3
  f64Str := "3.141592"
  
  i, err := strconv.Atoi(iStr)
  if err != nil { log.Fatal(err) }
  fmt.Println(i)  // 7
  
  s := strconv.Itoa(sInt)
  fmt.Println(s)  // 3
  
  f64, err := strconv.ParseFloat(f64Str, 64)
  if err != nil { log.Fatal(err) }
  fmt.Println(f64)  // 3.141592
}

'Go > Packages' 카테고리의 다른 글

[Go] Package log  (0) 2020.11.30
[Go] Package bufio  (0) 2020.10.31
[Go] Package errors  (0) 2020.10.29
[Go] Package math  (0) 2020.10.27
[Go] Package strings  (0) 2020.10.26

댓글