Skip to content

Instantly share code, notes, and snippets.

@ghulamostafa
Last active November 16, 2021 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghulamostafa/16bb972df69eece2eefebe9e21473763 to your computer and use it in GitHub Desktop.
Save ghulamostafa/16bb972df69eece2eefebe9e21473763 to your computer and use it in GitHub Desktop.
Reading from console in Go and cast to integer/int.
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter text: ")
text, _ := reader.ReadString('\n')
fmt.Println("The text is: " + text)
//Replace with \r\n if on Windows and \n if on Linux
text = strings.Replace(text, "\r\n", "", -1)
castedText, _ := strconv.Atoi(text)
fmt.Println(castedText)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment