Suppose you I want to get a number from stdin, using io.Reader.ReadString, and you want to convert this number to an integer.

Before being able to convert it to integer using strconv.Atoi, you have to remove the new line char.

How can you do so?

Solution

Using strings.TrimSuffix:

package main

import (
	"bufio"
	"os"
	"strings"
)

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

Did you know?

I have 7 premium training programs that will transform you, quickly, into an excellent Node/React/JS/Vue/Next.js/Svelte developer. Choose your own adventure. Practical lessons to learn the 80% of the things that you need to know, in 20% of the time!

⬇️ ⬇️ ⬇️

Check out all my courses now!

⬆️ ⬆️ ⬆️