Trimming strings in Go
Posted
by
user1263980
on Stack Overflow
See other posts from Stack Overflow
or by user1263980
Published on 2012-09-07T02:43:59Z
Indexed on
2012/09/07
3:38 UTC
Read the original article
Hit count: 104
go
I'm trying to read an entire line from the console (including whitespace), then process it. Using bufio.ReadString, the newline character is read together with the input, so I came up with the following code to trim the newline character:
input,_:=src.ReadString('\n')
inputFmt:=input[0:len(input)-2]+""
Is there a more idiomatic way to do this? That is, is there already a library that takes care of the ending null byte when extracting substrings for you?
(Yes, I know there is already a way to read a line without the newline character in go readline -> string but I'm looking more for elegant string manipulation.)
© Stack Overflow or respective owner