Last active
November 16, 2021 12:04
-
-
Save ghulamostafa/16bb972df69eece2eefebe9e21473763 to your computer and use it in GitHub Desktop.
Reading from console in Go and cast to integer/int.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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