Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang doc func parameters

Tags:

go

Reading the godoc doc. It does not specify how function parameters are documented.

What is the reason for omitting this?

like image 494
Justin Lin Avatar asked Jan 14 '18 18:01

Justin Lin


2 Answers

There is no explicit documentation of function parameters in godoc. Any necessary details not covered by the name and type of the parameter should go into the doc comment for the function. For examples, see every function in the standard library.

like image 125
Adrian Avatar answered Nov 06 '22 23:11

Adrian


Golang prefers a style wherein the function signature is 'self documenting', in that the combination of a parameter/argument name and it's type should be be largely explanatory. Additional information should be provided in the doc header in a natural language style. From the golang example.go

// splitExampleName attempts to split example name s at index i,
// and reports if that produces a valid split. The suffix may be
// absent. Otherwise, it must start with a lower-case letter and
// be preceded by '_'.
//
// One of i == len(s) or s[i] == '_' must be true.
func splitExampleName(s string, i int) (prefix, suffix string, ok bool) {
    if i == len(s) {
        return s, "", true
    }
    if i == len(s)-1 {
        return "", "", false
    }
    prefix, suffix = s[:i], s[i+1:]
    return prefix, suffix, isExampleSuffix(suffix)
}

Here we see that details about s and i are included in the summary description ahead of the function. Similarly, notes about the return values are included in that paragraph. This differs from Java or Python or other languages, which propose a more formal structure for each of these details. The reason for this is that Golang style is typically optimized for concision and flexibility, eschewing the prescriptive style-guide approach of other languages and relying on gofmt for the majority of the heavy lifting.

like image 37
Nathaniel Ford Avatar answered Nov 06 '22 23:11

Nathaniel Ford