Write Godoc for interface implementation by private struct type

771 Views Asked by At

I have written a library and its an Interface implementation with private struct. In godoc, struct bounded functions are not showing. I think it is because of an unexposed struct. But my functions are exposed with the interface. Is there any way to create good go doc for it? or should i have to explain function's behaviours on Interface deceleration section ?

I have added simple version of package i have written. here is the real Go library I need to write good documentation.

package printer

import "fmt"

// Printer is the interface to Print anything
type Printer interface {
    // Print is a wrapper of "fmt.Println"
    Print(string)
}

type printer struct {
    prefix string
}

// Print prints the sent string with prefix
func (p *printer) Print(s string) {
    fmt.Println(p.prefix,s)
}

// NewPrinter returns a Printer with sent prefix
func NewPrinter(prefix string) Printer{
    return &printer{
        prefix: prefix,
    }
}
1

There are 1 best solutions below

2
Jakub Dóka On

If you have to document methods of private struct, do it above function that returns it. In general you should also consider leaving good comment above interface method header. Go will not show documentation of something that is not accesable by user (directly) and is not smart enough to know that you can access it trough interface.