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

fmt: "default format" doesn't honor Stringer element of struct #9663

Closed
bupjae opened this issue Jan 22, 2015 · 1 comment
Closed

fmt: "default format" doesn't honor Stringer element of struct #9663

bupjae opened this issue Jan 22, 2015 · 1 comment

Comments

@bupjae
Copy link

@bupjae bupjae commented Jan 22, 2015

I run following program in playground ( http://play.golang.org/p/BLgzIQpj1g ):

package main

import "fmt"

type testEnum int

const (
    test0 testEnum = iota
    test1
    test2
    test3
)

func (t testEnum) String() string {
    switch t {
    case test0:
        return "test0"
    case test1:
        return "test1"
    case test2:
        return "test2"
    case test3:
        return "test3"
    default:
        return fmt.Sprintf("testEnum(%d)", int(t))
    }
}

func main() {
    fmt.Println(struct{ a testEnum }{a: test1})
}

Expect: {test1}
Actual: {1}

This behavior is inconsistant with other "compound" objects (array, slice, or map).

@rjeczalik
Copy link

@rjeczalik rjeczalik commented Jan 22, 2015

Your struct literal uses unexported member, that's why it's not inspected for fmt.Stringer. Export the member or embed testEnum instead and it'll work.

@bradfitz bradfitz closed this Jan 22, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.