Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / Convert string to integer type in Go?

Convert string to integer type in Go?

August 20, 2021 by James Palmer

For example,
package main

import (
“flag”
“fmt”
“os”
“strconv”
)

func main() {
flag.Parse()
s := flag.Arg(0)
// string to int
i, err := strconv.Atoi(s)
if err != nil {
// handle error
fmt.Println(err)
os.Exit(2)
}
fmt.Println(s, i)
}

Converting Simple strings
The easiest way is to use the strconv.Atoi() function.
Note that there are many other ways. For example fmt.Sscan() and strconv.ParseInt() which give greater flexibility as you can specify the base and bitsize for example. Also as noted in the documentation of strconv.Atoi():

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

Here’s an example using the mentioned functions (try it on the Go Playground):
flag.Parse()
s := flag.Arg(0)

if i, err := strconv.Atoi(s); err == nil {
fmt.Printf(“i=%d, type: %Tn”, i, i)
}

if i, err := strconv.ParseInt(s, 10, 64); err == nil {
fmt.Printf(“i=%d, type: %Tn”, i, i)
}

var i int
if _, err := fmt.Sscan(s, &i); err == nil {
fmt.Printf(“i=%d, type: %Tn”, i, i)
}

Output (if called with argument “123”):
i=123, type: int
i=123, type: int64
i=123, type: int

Parsing Custom strings
There is also a handy fmt.Sscanf() which gives even greater flexibility as with the format string you can specify the number format (like width, base etc.) along with additional extra characters in the input string.
This is great for parsing custom strings holding a number. For example if your input is provided in a form of “id:00123” where you have a prefix “id:” and the number is fixed 5 digits, padded with zeros if shorter, this is very easily parsable like this:
s := “id:00123”

var i int
if _, err := fmt.Sscanf(s, “id:%5d”, &i); err == nil {
fmt.Println(i) // Outputs 123
}

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2025 · News Pro Theme on Genesis Framework · WordPress · Log in