Go, remove the io.Reader.ReadString newline char

Go: remove the new line char from a string acquired using io.Reader.ReadString

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?


Using strings.TrimSuffix:

package main

import (

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

Download my free Go Handbook!

Download my free Programming Ebooks!