I've stumbled upon a function that defines its own request and response types.
func doSomething() {
type request struct {
resourceID string
}
type response struct {
resourceContents *Data
}
request := initializeRequest()
result := dispatchRequest(request)
...
}
I think this has the great advantage of documenting the structure of the request and response right in the body of the function. The convenience of this is particularly evident when many such functions are in the same file and they all have a different type for their request and response structs: defining the types outside of the function means that I have to name them differently.
I'm concerned about the cost of this, though: Is it much more expensive to have a function call declare its own type vs declaring that type in the package scope?
Also, is this approach idiomatic?