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

Converting a byte array filled with 0's to a string results in non-empty string (despite appearances) #39836

Closed
chabad360 opened this issue Jun 24, 2020 · 3 comments

Comments

@chabad360
Copy link

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

$ go version
go version go1.14.4 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/mendel/.cache/go-build"
GOENV="/home/mendel/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/mendel/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/mendel/Documents/Projects/Untitled/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-build439515910=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
	"fmt"
)

func main() {
	b := [32]byte{}
	fmt.Printf("b: %v, string(b[:]): %v, string(b[:]) == \"\": %v", b, string(b[:]), string(b[:]) == "")
}

https://play.golang.org/p/Yg03ksgALwE

What did you expect to see?

b: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], string(b[:]): , string(b[:]) == "": true

What did you see instead?

b: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], string(b[:]): , string(b[:]) == "": false
@chabad360 chabad360 changed the title Converting a byte array filled with 0 to a string results in non-empty string Converting a byte array filled with 0's to a string results in non-empty string Jun 24, 2020
@chabad360 chabad360 changed the title Converting a byte array filled with 0's to a string results in non-empty string Converting a byte array filled with 0's to a string results in non-empty string (despite appearances) Jun 24, 2020
@randall77
Copy link
Contributor

This is intended. You get a string with 32 \0 characters in it, not the empty string.

@chabad360
Copy link
Author

chabad360 commented Jun 24, 2020

I understand that, but then how would I check to see if the string contains a \0 (irl the array size is some what random)?

@ianlancetaylor
Copy link
Contributor

Sorry, when you say "check to see if the string contains that" I don't know what you mean by "that".

In general we do not use the issue tracker for questions about how Go works. You will get a better and faster answer on a forum. Please see https://golang.org/wiki/Questions. Thanks.

@golang golang locked and limited conversation to collaborators Jun 24, 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

4 participants