potential gofmt style inconsistency

已查看 540 次
跳至第一个未读帖子

Ryan Macy

未读,
2013年1月15日 23:01:152013/1/15
收件人 golan...@googlegroups.com
When I run gofmt, it puts a space before and after the plus in Println,
but not on other functions like ListenAndServe.

fmt.Println("Listening for incoming requests on: " + os.Getenv("PORT"))
err := http.ListenAndServe(":"+os.Getenv("PORT"), nil)

Is that intended-- I can file a bug if not.

_Ryan

Andrew Gerrand

未读,
2013年1月15日 23:23:352013/1/15
收件人 Ryan Macy、golan...@googlegroups.com

On 16 January 2013 15:01, Ryan Macy <ry...@hackery.io> wrote:
Is that intended-- I can file a bug if not.

It's intended.

The Println call takes one argument, so gofmt uses spaces to clearly identify the string concatenation.
The ListenAndServe call takes two argument and spaces are already used to separate arguments, so gofmt doesn't use spaces around the concatenation to clearly keep all parts of the first argument together.

Incidentally, the first call would probably be more efficient if you avoid the concatenation:

 fmt.Println("Listen:", os.Getenv("PORT"))

Andrew

Ryan Macy

未读,
2013年1月15日 23:32:372013/1/15
收件人 Andrew Gerrand、golan...@googlegroups.com
Andrew Gerrand wrote:


On 16 January 2013 15:01, Ryan Macy <ry...@hackery.io


Ah, Thanks for the clarification.

_Ryan

Matt Cudmore

未读,
2013年4月23日 17:31:402013/4/23
收件人 golan...@googlegroups.com
I went looking to see if someone had already asked about this.

it also shows up in this example:

fmt.Println("a"+"b", "c", 1+2)
fmt.Println([]interface{}{"a" + "b", "c", 1 + 2}...)

in the second line, gofmt puts spaces between everything.

not a big issue though, the inconsistency simply catches the eye and doesn't immediately make sense.

Dominik Honnef

未读,
2013年4月24日 19:42:472013/4/24
收件人 golan...@googlegroups.com
Matt Cudmore <cudmo...@gmail.com> writes:

> I went looking to see if someone had already asked about this.
>
> it also shows up in this example:
>
> fmt.Println("a"+"b", "c", 1+2)
> fmt.Println([]interface{}{"a" + "b", "c", 1 + 2}...)
>
> in the second line, gofmt puts spaces between everything.
>
> not a big issue though, the inconsistency simply catches the eye and
> doesn't immediately make sense.

It's somewhat inconsistent, but only in that function calls and value
literals have different rules.

On Wednesday, 16 January 2013 00:01:15 UTC-4, RMacy wrote:
> When I run gofmt, it puts a space before and after the plus in Println,
> but not on other functions like ListenAndServe.
>
> fmt.Println("Listening for incoming requests on: " +
> os.Getenv("PORT"))
> err := http.ListenAndServe(":"+os.Getenv("PORT"), nil)
>
> Is that intended-- I can file a bug if not.
>
> _Ryan
>

gofmt uses operator spacing (or the lack thereof) to signify operator
precedence.

This also applies to using operators in function calls with only one
argument and more than one argument.

Also see http://play.golang.org/p/PCyV62nKhH

rat...@google.com

未读,
2017年10月4日 14:36:312017/10/4
收件人 golang-nuts
Consider:

foo(1+2)
foo(1+2, 3) 

After formatting it becomes
foo(1 + 2)
foo(1+2, 3)

This might be working as intended, but it's awful. Any chance this might be reconsidered?

Ian Lance Taylor

未读,
2017年10月4日 14:50:262017/10/4
收件人 rat...@google.com、golang-nuts
At this point, I doubt it. It is indeed working as intended.

Ian
回复全部
回复作者
转发
0 个新帖子