Keeping track of function parameters

I am confused about something with this auto formatting stuff Go forces us to deal with. Here is an example of the way I declare functions with many parameters in other languages:

func myfunction( param1 string,
                           param2 string,
                           param3 string... )

This allows me to see the function parameters easily without scrolling left and right to read them all. I also make calling the functions the same because I can easily make sure everything matches up. But in Go it appears you are forced to put everything on a single long line with no line breaks.

Am I missing something?

Glenn

gofmt does this for you:

package main

import (
    "fmt"
)

func foo(paraaaaaaaaaaaaaaaaaaaaam1 int,
    paraaaaaaaaaaaaaaaaaaaaam2 int,
    paraaaaaaaaaaaaaaaaaaaaam3 int,
    paraaaaaaaaaaaaaaaaaaaaam4 int,
    paraaaaaaaaaaaaaaaaaaaaam5 int,
    paraaaaaaaaaaaaaaaaaaaaam6 int,
    paraaaaaaaaaaaaaaaaaaaaam7 int,
    paraaaaaaaaaaaaaaaaaaaaam8 int,
    paraaaaaaaaaaaaaaaaaaaaam9 int,
    paraaaaaaaaaaaaaaaaaaaaam10 int) string {
    return "foo"
}
func main() {
    fmt.Println(foo(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
}
1 Like

Not on my system it doesn’t and if it did you should be able to manually do that. However, if you do and you try to compile you get an error about the if statement line not ending with an {

You can see in the playground that it does, it even allows to mix a couple inline and a third in a new line. Just click format in the example: Go Playground - The Go Programming Language

What if are you talking about? There’s neither an if in your example or in lutzhorn’s one. Anyway, ifs must contain the opening bracket in the same line and the same applies to functions/methods. This is not just a style suggestion, it’s enforced by the compiler. The idea behind it is to keep style discussions away from Go code bases as the decision was already made for you, and it helps with tools such as gofmt. I won’t argue in favor or against it, just know that it works this way and at least I got used to it pretty quickly.

1 Like

I apologize. I meant func not if. However, I just went back and did it again and this time it worked. So I am incorrect and not sure what I did different the other 3 times I tried this.

Thanks,

Glenn

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.