This question already has an answer here:
I find some function with no function body in Go. I know it means external function in Go. But where can I find the function boby?
type Creator func(*Beat, *common.Config) (Beater, error)
I also find a string in Go struct. What does it mean?
type BeatConfig struct {
// output/publishing related configurations
Output common.ConfigNamespace `config:"output"`
}
All of the above code can be found in elasticsearch beats: https://github.com/elastic/beats/blob/master/libbeat/beat/beat.go
</div>
This:
type Creator func(*Beat, *common.Config) (Beater, error)
Is not a function declaration, it's a type declaration. It creates a new type (with a function type as its underlying type), it does not declare any functions, and so no function body is required, it wouldn't make sense and you cannot provide a body in there.
You can use it for example to create a variable of that type, and store an actual function value which has the same underlying type.
Example:
type Creator func(*Beat, *common.Config) (Beater, error)
func SomeCreator(beat *Beat, config *common.Config) (Beater, error) {
// Do something
return nil, nil
}
func main() {
var mycreator Creator = SomeCreator
// call it:
beater, err := mycreator(&Beat{}, &common.Config{})
// check err, use beater
}
And this field declaration:
Output common.ConfigNamespace `config:"output"`
Contains a tag declaration for the Output
field. For more details, see What are the use(s) for tags in Go?
As @icza said, Creator
is a type declaration.
In your case, each implementation of Beat
has implemented Creator
interface. Here is beats's document.