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

Should function signature formatting be applied to this? #145

Open
ixje opened this issue Aug 13, 2024 · 4 comments
Open

Should function signature formatting be applied to this? #145

ixje opened this issue Aug 13, 2024 · 4 comments

Comments

@ixje
Copy link

ixje commented Aug 13, 2024

Let's assume the following function exceeds the maximum line-length

func foo(name1 string, name2 string, name3 bool, name4 []byte) (*[]byte, error) {
	return nil, nil
}

golines will then reformat it to

func boo(
	name1 string,
	name2 string,
	name3 bool,
	name4 []byte,
) (*[]byte, error) {
	return nil, nil
}

This suggests to me there should be at most 2 function signature styles

  1. single line (if within max line length)
  2. multi-line with each param on a separate line (if exceeding max line length)

However, if we take something like this

func foo(
    name1 string, name2 string, name3 bool, name4 []byte
) (*[]byte, error) {
	return nil, nil
}

or this

func foo(name1 string, name2 string, name3 bool, 
    name4 []byte) (*[]byte, error) {
	return nil, nil
}

golines will not format the function signature. Is this expected behaviour or should it actually format it using the multi-line params approach?

@ccoVeille
Copy link

Good question!

@guettli
Copy link

guettli commented Jan 17, 2025

Yes, I would like that, too: either all arguments on one line XOR one line per argument with first arg being on a new line.

@guettli
Copy link

guettli commented Jan 17, 2025

@ixje are you sure that it is broken? This works fine:

package main

func foo(loooooooongname1 string, loooooooongint1 int, loooooooongname2 string, loooooooongint2 int) {
 return
}

func main() {
 foo("a", 1, "b", 2)
}

output:

❯ golines tmp/t/main.go
package main

func foo(
    loooooooongname1 string,
    loooooooongint1 int,
    loooooooongname2 string,
    loooooooongint2 int,
) {
    return
}

func main() {
    foo("a", 1, "b", 2)
}

@ixje
Copy link
Author

ixje commented Jan 18, 2025

@guettli for your example sure, but that was not what I tried pointing out

This has a length of 98

func foo(loooooooongname1 string, loooooooongint1 int, loooooooongname2 string, looongint2 int) {
	return
}

but could have been written as this (let's ignore how we might have gotten to such format)

func foo(
	loooooooongname1 string, loooooooongint1 int, loooooooongname2 string, looongint2 int,
	) {
	return
}

Now golines doesn't format it

$ golines golines.go 
package main

func foo(
        loooooooongname1 string, loooooooongint1 int, loooooooongname2 string, looongint2 int,
) {
        return
}

func main() {
        foo("a", 1, "b", 2)
}

I think it ideally should be formatted as the first snippet, because it fits in that length.

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