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

encoding/base64: shows wrong output #59408

Closed
akshaybabloo opened this issue Apr 3, 2023 · 3 comments
Closed

encoding/base64: shows wrong output #59408

akshaybabloo opened this issue Apr 3, 2023 · 3 comments

Comments

@akshaybabloo
Copy link

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

$ go version
go version go1.20.2 windows/amd64

Does this issue reproduce with the latest release?

Yes, and works fine with 1.16

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

go env Output
$ go env

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\gollaha\AppData\Local\go-build
set GOENV=C:\Users\gollaha\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\gollaha\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\gollaha\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.20.2
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=C:\Users\gollaha\Code\personal\rex\rex-cli\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\gollaha\AppData\Local\Temp\go-build1860088289=/tmp/go-build -gno-record-gcc-switches

What did you do?

I am trying to convert a struct to base64 string with the following code:

package main

import (
	"bytes"
	"encoding/base64"
	"encoding/json"
	"fmt"
)

type testVariable struct {
	Out string `json:"out"`
}

func EncodeBase64(v interface{}) (string, error) {
	var buf bytes.Buffer

	encoder := base64.NewEncoder(base64.URLEncoding, &buf)
	defer encoder.Close()
	err := json.NewEncoder(encoder).Encode(v)
	if err != nil {
		return "", err
	}
	return buf.String(), nil
}

func main() {
	example := testVariable{
		Out: "Hello World!",
	}
	out, err := EncodeBase64(example)
	if err != nil {
		panic(err)
	}
	fmt.Println(out)
}

Go Play link - https://go.dev/play/p/3liPKR7H0DG <- this shows wrong output
https://go.dev/play/p/r9sXiRSXrwp <- This shows the correct output

What did you expect to see?

The output should be eyJvdXQiOiJIZWxsbyBXb3JsZCEifQo=

What did you see instead?

Instead, I get eyJvdXQiOiJIZWxsbyBXb3JsZCEi

The reason this is happening is because of the placement of defer encoder.Close(), if you move encoder.Close() after err block in EncodeBase64 it outputs the correct string.

@akshaybabloo akshaybabloo changed the title encoding/base64: encoding/base64: shows wrong output Apr 3, 2023
@seankhliao
Copy link
Member

working as intended
encoder is documented as needing to be closed to flush data
and defers go after the the return

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Apr 3, 2023
@akshaybabloo
Copy link
Author

So what is the right way to do it?

@ianlancetaylor
Copy link
Contributor

@akshaybabloo Call encoder.Close() after calling Encode but before calling buf.String.

If you have any further questions, please see https://go.dev/wiki/Questions. Thanks.

@golang golang locked and limited conversation to collaborators Apr 2, 2024
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