Search code examples
gogodoc

Godooc won't read my code, or godoc can't read anonymouse function?


Recently I tried to document my code, but I had some trouble using godoc because there's some function that didn't came up when I ran godoc -http:localhost:6060

This is what my code looks like:

type MongoDBInterface interface {
    ExecuteTransaction(operation func(mongoClient MongoDBInterface) error) error
    Count(tableName string, clause bson.M) (int, error)
    Distinct(tableName, fieldName string, clause bson.M) ([]interface{}, error)
    InsertOrUpdate(tableName string, clause bson.M, data models.BaseModelInterface) (primitive.ObjectID, error)
    InsertOrUpdateFields(tableName string, clause bson.M, data interface{}) (primitive.ObjectID, error)
    Insert(tableName string, data models.BaseModelInterface) (primitive.ObjectID, error)
    Update(tableName string, clause bson.M, data models.BaseModelInterface) error
    UpdateFields(tableName string, clause bson.M, data interface{}) error
    FindOne(tableName string, clause, opt bson.M, result interface{}) error
    FindMany(tableName string, clause, opt bson.M, result interface{}) error
    Truncate(tableName string) error
    Delete(tableName string, clause bson.M) error
    Aggregate(tableName string, pipelines interface{}, result interface{}) error
    EnsureCollections() error
}

type mongoDB struct {
    session              mongo.Session
    db                   *mongo.Database
    ctx                  context.Context
    isTransactionEnabled bool
    isConnected          bool

    connString string
}

// NewMongoDB definition
func NewMongoDB() MongoDBInterface {
    mongoClient := new(mongoDB)
    mongoClient.ctx = context.Background()

    dbHost := os.Getenv("DB_HOST")
    if dbHost == "" {
        dbHost = "localhost"
    }
    dbUser := os.Getenv("DB_USERNAME")
    if dbUser == "" {
        dbUser = "dbadmin"
    }
    dbPswd := os.Getenv("DB_PASSWORD")
    if dbPswd == "" {
        dbPswd = "dbpassword"
    }
    dbName := os.Getenv("DB_NAME")
    if dbName == "" {
        dbName = "dbname"
    }
    dbAuth := os.Getenv("DB_AUTH")
    if dbAuth == "" {
        dbAuth = "admin"
    }
    dbMode := os.Getenv("DB_MODE")
    if dbMode == "" {
        dbMode = "admin"
    }

    // temporary
    connString := fmt.Sprintf("mongodb+srv://%s:%s@%s/%s?retryWrites=true&w=majority", dbUser, dbPswd, dbHost, dbName)
    mongoClient.connString = connString

    return mongoClient
}

// connect to mongodb server
func (s *mongoDB) connect() error {
    // get query string from env,
    // then parse it to get db name

    // connString := os.Getenv("MONGODB_CONN_STRING")
    connString := s.connString
    log.Println("ConnString =>", connString)

    parts := strings.Split(connString, "/")
    dbName := strings.Split(parts[len(parts)-1], "?")[0]

    // prepare options object for connecting to mongodb
    opt := options.Client()
    opt.ApplyURI(connString)

    // set the timeout info from data defined in the env
    // timeout, _ := strconv.Atoi(os.Getenv("MONGODB_TIMEOUT_IN_SECOND"))
    timeout := 120
    timeoutDuration := time.Duration(timeout) * time.Second
    opt.ConnectTimeout = &timeoutDuration

    // create client object
    client, err := mongo.NewClient(opt)
    if err != nil {
        log.Println(err.Error())
        return err
    }

    // connect to the db server
    err = client.Connect(context.Background())
    if err != nil {
        log.Println(err.Error())
        return err
    }

    // start new session
    session, err := client.StartSession()
    if err != nil {
        log.Println(err.Error())
        return err
    }

    // store session and db info into props
    s.session = session
    s.db = client.Database(dbName)
    s.isConnected = true

    log.Println("Connected to database")

    return nil
}

The problem is godoc will never render func (s *mongoDB) connect() error but I need it to be documented, can you guys explain to me what's going on with godoc? Or maybe you can give me some solutions and tips for documenting Go code.


Solution

  • You can refer this doc: https://pkg.go.dev/golang.org/x/tools/cmd/godoc

    The presentation mode of web pages served by godoc can be controlled with the "m" URL parameter; it accepts a comma-separated list of flag names as value:
    
    - all   show documentation for all declarations, not just the exported ones
    - methods   show all embedded methods, not just those of unexported anonymous fields
    - src   show the original source code rather than the extracted documentation
    - flat  present flat (not indented) directory listings using full paths
    
    For instance, https://golang.org/pkg/math/big/?m=all shows the documentation for all (not just the exported) declarations of package big. 
    

    ?m=all documents all declaration including the non-exported methods