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

log/slog: LogValue not called pointer receiver vs value #60962

Closed
mvrhov opened this issue Jun 23, 2023 · 3 comments
Closed

log/slog: LogValue not called pointer receiver vs value #60962

mvrhov opened this issue Jun 23, 2023 · 3 comments

Comments

@mvrhov
Copy link

mvrhov commented Jun 23, 2023

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

$ go version
go version go1.21rc2 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

go env
GO111MODULE=''
GOARCH='amd64'
GOBIN='/srv/go/bin'
GOCACHE='/home/miha/.cache/go-build'
GOENV='/home/miha/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/srv/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/srv/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.21rc2'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3497984750=/tmp/go-build -gno-record-gcc-switches'

What did you do?

type Foo struct {
	V1 uint64
	V2 uint64
}

func (f *Foo) String() string {
	return fmt.Sprintf("%d %d", f.V1, f.V2)
}

func (f *Foo) LogValue() slog.Value {
	return slog.GroupValue(slog.String("string", "s"),
		slog.Group("i", slog.Uint64("v1", f.V1), slog.Uint64("v2", f.V2)))
}

func init() {
	t := Foo{
		V1: 345_678,
		V2: 10_000,
	}

	l := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{}))
	l.LogAttrs(context.Background(), slog.LevelInfo, "test value", slog.Any("t", t))
	l.LogAttrs(context.Background(), slog.LevelInfo, "test pointer", slog.Any("t", &t))
}

What did you expect to see?

{"time":"2023-06-23T12:42:53.827957997+02:00","level":"INFO","msg":"test value","t":{"string":"s","i":{"v1":345678,"v2":10000}}}
{"time":"2023-06-23T12:42:53.828082686+02:00","level":"INFO","msg":"test pointer","t":{"string":"s","i":{"v1":345678,"v2":10000}}}

What did you see instead?

{"time":"2023-06-23T12:42:53.827957997+02:00","level":"INFO","msg":"test value","t":{"V1":345678,"V2":10000}}
{"time":"2023-06-23T12:42:53.828082686+02:00","level":"INFO","msg":"test pointer","t":{"string":"s","i":{"v1":345678,"v2":10000}}}

@cuonglm
Copy link
Member

cuonglm commented Jun 23, 2023

Seems working as expected to me, &t is a slog.LogValuer, but t is not.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jun 23, 2023
@mvrhov
Copy link
Author

mvrhov commented Jun 29, 2023

If this is expected behavior, Then the go tour should be changed/ fixed, because it's saying to NOT have mixed methods on type, e.g on value and pointer receiver methods.

@cuonglm
Copy link
Member

cuonglm commented Jun 29, 2023

If this is expected behavior, Then the go tour should be changed/ fixed, because it's saying to NOT have mixed methods on type, e.g on value and pointer receiver methods.

I'm not sure it's related.

  • What go tour said mean you should declare all methods of a type using either only pointer receiver, or only value receiver. This is true in this example, all methods are declared with pointer receiver.

  • The issue here is that method with value receiver of type T will be available for type *T, but not vice versa. That mean *Foo is a LogValuer, but Foo is not, because Foo don't have LogValue method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants