FAQ
In the code below in the Invoice struct Time time.Time line did not
align with the other types after running "go fmt". This is not
reproducible on the playground, and I using tip if that helps. Is this
just my system or can anyone else reproduce it?

Andrew

package main

import (
"time"
)

// Holds the name and balance of an account.
type Account struct {
Name string
Balance float64
}

// Represents a transaction on an account.
type Invoice struct {
Amount float64
Subject string
Details string
Tags []string
Account
Time time.Time
}

func main() {
fmt.Println(Invoice{10.00, "food", "lunch with gopher",
[]string{"food", "restaurant"}, Account{"cash", 20.25}, time.Now()})
}

--

Search Discussions

  • Dmitri Shuralyov at Oct 25, 2012 at 3:36 am
    The type for Account is missing? After you fix that error, it formats
    correctly.

    --
  • Dmitri Shuralyov at Oct 25, 2012 at 3:41 am
    I'm sorry, I failed to notice the type Account defined above. I'm not used
    to not having an edit button; I should refrain from posting so eagerly (as
    I'm used to, and then verify things after the post is live).

    Yes, I can reproduce the problem on go1.0.3/osx. The above code compiles
    and yet gofmt doesn't format the Time time.Time line as expected.

    --
  • Peter S at Oct 25, 2012 at 4:16 am
    gofmt alignment does not work by syntactical blocks, but by blocks of
    consecutive lines with the same format (e.g. "identifier type"). Because of
    this, if you insert a line without type (or a blank line, or a line with
    just a comment), lines above and below will be treated as separate "blocks"
    (for formatting purposes).

    I believe this is a design decision (not a bug). One of the benefits is
    that it allows you to break the fields in a struct into several "groups",
    so for example adding a long identifier to one group does not cause a
    change in the alignment of all fields (which could be very annoying when
    checking diffs). But in your case, if you do want all types to be aligned,
    you could move embedded field(s) to the front (or the end).

    The algorithm used is that of the text/tabwriter package, for details check
    these pages:
    http://golang.org/pkg/text/tabwriter/
    http://nickgravgaard.com/elastictabstops/index.html

    The playground seems to align to tab stops only (I'm not quite sure why),
    but other than that it is the same algorithm, so you can reproduce the same
    effect by including an identifier longer than 7 letters:
    http://play.golang.org/p/vzoBSLeMx3 (I just changed "Subject" to
    "Subjects".)

    Peter

    On Thu, Oct 25, 2012 at 11:28 AM, Andrew O'Neill wrote:

    In the code below in the Invoice struct Time time.Time line did not
    align with the other types after running "go fmt". This is not
    reproducible on the playground, and I using tip if that helps. Is this
    just my system or can anyone else reproduce it?

    Andrew

    package main

    import (
    "time"
    )

    // Holds the name and balance of an account.
    type Account struct {
    Name string
    Balance float64
    }

    // Represents a transaction on an account.
    type Invoice struct {
    Amount float64
    Subject string
    Details string
    Tags []string
    Account
    Time time.Time
    }

    func main() {
    fmt.Println(Invoice{10.00, "food", "lunch with gopher",
    []string{"food", "restaurant"}, Account{"cash", 20.25}, time.Now()})
    }

    --

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 25, '12 at 2:29a
activeOct 25, '12 at 4:16a
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase