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: verb override using tags on struct fields. #6115

Closed
gopherbot opened this issue Aug 12, 2013 · 5 comments
Closed

fmt: verb override using tags on struct fields. #6115

gopherbot opened this issue Aug 12, 2013 · 5 comments
Assignees

Comments

@gopherbot
Copy link

@gopherbot gopherbot commented Aug 12, 2013

by BeMasher:

I find that often when I'm writing types I need to apply special fmt formatting to
specific fields in structs while leaving the rest as the default "v" fmt verb.
Implementing Stringer on the type becomes cumbersome when I only need to modify the
formatting for one or two fields in a struct.

For example the following type requires a tedious String function where adding or
removing fields requires modification in the type and the String function:

type Foo {
    Bar  []byte
    Baz1 uint32
    Baz2 uint32
    Baz3 uint32
    Baz4 uint32
    Baz5 uint32
}

func (f Foo) String() string {
    return fmt.Sprintf("{Bar:%s Baz1:%d Baz2:%d Baz3:%d Baz4:%d Baz5:%d}", f.Bar, f.Baz1, f.Baz2, f.Baz3, f.Baz4, f.Baz5)
}

One solution is to add a tag to the Bar field specifying the verb to use since it is the
only field that needs to be formatted differently from the standard "v" verb.
This allows me to use fmt functions that don't require a format string without
constantly having to update the String method.

type Foo {
    Bar  []byte `fmt:"s"`
    Baz1 uint32
    Baz2 uint32
    Baz3 uint32
    Baz4 uint32
    Baz5 uint32
}

Printing using fmt.Println (or other non-formatting) then produces the desired result
without needing to implement Stringer for the type.

Need to determine when tags will be used or ignored, particularly when a format string
is supplied.
@GeertJohan
Copy link
Contributor

@GeertJohan GeertJohan commented Aug 12, 2013

Comment 1:

Interesting idea. Giving it some thoughts.. I think the tag should contain the % for
completeness. So your example would then become `fmt:"%s"`. This will keep formats like
"% x" and "%0d" readable and will be more recognizable.
@cznic
Copy link
Contributor

@cznic cznic commented Aug 12, 2013

Comment 2:

Are we going to write this eventually?
type foo struct {
        bar qux `"fmt:whatever" "json:whatever" "xml:whatever" "bson:whatever" "mgo:whatever" "gob:whatever" "mydsl:whatever"`
        baz int ...
}
@adg
Copy link
Contributor

@adg adg commented Aug 12, 2013

Comment 3:

I pretty much only print structs when debugging. Is that what you use it for, too? If
so, I don't think this extra mechanism is worth it.

Labels changed: added feature, priority-someday, removed priority-triage, go1.2maybe.

Owner changed to @robpike.

Status changed to Thinking.

@gopherbot
Copy link
Author

@gopherbot gopherbot commented Aug 12, 2013

Comment 4 by BeMasher:

It makes sense to have the tag obey all the verb formatting like the "%" prefix
suggested.
Going through the fmt source, it looks like it would be reasonable to ignore tags when
either the go syntax "%#v" verb is used and any verb other than "v".
I've got most of the logic written already, just need to write some tests.
@robpike
Copy link
Contributor

@robpike robpike commented Aug 13, 2013

Comment 5:

After sleeping on it I've decided I don't like this. There are already two mechanisms to
control printing of values and, although perhaps less convenient, they cover the job.
Let's not push for more use of tagged structures.

Status changed to WontFix.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
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
5 participants
You can’t perform that action at this time.