import "fmt"
func add(x: int, y: int) {
return x + y
}
func main() {
fmt.Printf("%d", add(x: 3, y: 7))
}
go get github.com/elliotchance/go-named-params
You need to include two lines at the top of each file:
//go:generate $GOPATH/bin/go-named-params $GOFILE
// +build ignore
Then use go generate
to process the files:
go generate
go build
Using functions with named parameters makes code much easier to read. Consider the following code:
sayHello("Elliot", false)
Can you guess what the false
is for? You have to find the function definition
for sayHello
and look at the parameters name which may be located in a file
that's hard to find if your not using an IDE:
func sayHello(name string, alreadyGreeted bool)
Now imagine the same invocation written with named arguments:
sayHello(name: "Elliot", alreadyGreeted: false)
Much more clear! You don't have to goto the function definition to see what the
arguments are, but if you did you would see it is a very similar syntax just
adding :
for each argument:
func sayHello(name: string, alreadyGreeted: bool)
There are some important things to know about this implementation:
Most notably this is not native Go syntax. You will have to run go generate
before you can build or run the files.
Go does not support method overloading (where multiple methods can be defined with the same name but different signatures) for good reason. However, when using named parameters they become part of the method name to make it unique. Here is what it looks like when it is translated:
func sayHello(name: string, alreadyGreeted: bool) //func sayHello_name_alreadyGreeted(name string, alreadyGreeted bool) ```
The function invocation does not need to know about the definition because they both contain the argument names. This is very important in being able to translate the called without having to pass through any external source, or even pass through an AST. It is literally a regular expressions replace.
All code generated is undoable. If you need to remove the named arguments
from production code so that it becomes native Go you can parse the files and
operate on the results. If you relly want to strip out all traces you can use
gofmt -r 'sayHello_name_alreadyGreeted -> sayHello' -d ./
Since the parameters become part of the method name, you can use this for appropriate overloading:
func writeToFile(path: string, fromInt: int) func writeToFile(path: string, fromString: string) ```