0

最近我试图记录我的代码,但是我在使用时遇到了一些麻烦,godoc因为我运行时没有出现一些功能godoc -http:localhost:6060

这就是我的代码的样子:

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
}

问题是godoc永远不会渲染func (s *mongoDB) connect() error,但我需要记录下来,你们能向我解释发生了什么godoc吗?或者你可以给我一些解决 Go 代码文档的方法和技巧。

4

1 回答 1

0

你可以参考这个文档: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记录所有声明,包括非导出方法

于 2021-04-28T05:01:02.647 回答