Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unknown escape sequence on a technically valid escape sequence #43337

Closed
scrouthtv opened this issue Dec 23, 2020 · 1 comment
Closed

Unknown escape sequence on a technically valid escape sequence #43337

scrouthtv opened this issue Dec 23, 2020 · 1 comment

Comments

@scrouthtv
Copy link

scrouthtv commented Dec 23, 2020

TLDR: \033[ works as expected, \e[ yields unknown escape although both do the same thing.

What version of Go are you using (go version)?

$ go version
go version go1.15.6 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/lenni/.cache/go-build"
GOENV="/home/lenni/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/lenni/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/lenni/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/lenni/git/easy-copy/src/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build736783570=/tmp/go-build -gno-record-gcc-switches"

What did you do?

On Linux (and recent Windows versions), text on the console can be colored with escape sequences of the form

\e   |       | [0-8 21-28] for text attributes       |
\033 | + [ + | [30-39,90-99] for foreground colors   | + m
             | [40-49,100-109] for background colors |

For more information, see https://misc.flogisoft.com/bash/tip_colors_and_formatting and https://en.wikipedia.org/wiki/Escape_sequences_in_C#ref_Note1.

For example, both \e[31;44m and \033[31;44m should create red text on blue background.
This does also work in C:
image

#include 

int main(int argc, char** argv) {
printf("\e[31;44mHello\e[0m\n");
printf("--\n");
printf("\033[31;44mHello again\033[0m\n");
}

However, trying to create string which contains \e yields a compiler error: https://play.golang.org/p/5XhK4vANSqI
The very same code with \033 works can compile and works as expected: https://play.golang.org/p/Wv3sJZBjrVI
image

When working with these examples, keep in mind that there are two escape sequences in each line, the one in the front, setting the color, and the one at the end, clearing the color.

What did you expect to see?

I would expect both \e and \033 to work the same way as they are used to do the same thing.
If someone is trying to colorize the console output of their tools, and they try to use \e, which fails, it feels counterintuitive for \033 to work.

What did you see instead?

A string containing \e yields a compiler error, whilst a string containing \033 works as expected.

This may somewhat be related to #11575. I don't know which category this belongs to.

@ianlancetaylor
Copy link
Contributor

ianlancetaylor commented Dec 23, 2020

The escape sequences supported in Go string literals are defined at https://golang.org/ref/spec#Rune_literals. The escape sequence \e is not supported by Go.

It is also not supported by standard C, although it is a common addition. This is mentioned on the Wikipedia page you cite, at https://en.wikipedia.org/wiki/Escape_sequences_in_C#Non-standard_escape_sequences .

Closing because this is not a bug.

@golang golang locked and limited conversation to collaborators Dec 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants