最近我试图记录我的代码,但是我在使用时遇到了一些麻烦,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 代码文档的方法和技巧。