Go and delete the io.Reader.ReadString newline

Go to: delete the new line char from the string obtained using io.Reader.ReadString

Suppose you want to get a number from stdin using the following methodio.Reader.ReadStringAnd you want to convert this number to an integer.

Before being able to convert it to an integer usingstrconv.Atoi, You must delete the new line char.

How can you do this?

solution

usestrings.TrimSuffix:

package main

import ( “bufio” “os” “strings” )

func main() { reader := bufio.NewReader(os.Stdin) text, _ := reader.ReadString(’\n’) text = strings.TrimSuffix(text, “\n”) }


More tutorials: